【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)

题干:

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case:

The first contains two integers N, M (1 ≤ N, M ≤ 105).

Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input

2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2

Sample Output

4 3
4 4

题目大意:

给你一个无向图,每条边有一个距离c和花费d,叫你选一些边,使得点0到其他所有点的距离之和最小,其次,使总花费最小。

解题报告:

   因为总题最优解一定满足局部局部解,所以距离和最小就可以用求最短路来求解,最后统计个答案就可以。对于第二个权值的维护,可以在Dijkstra的同时贪心第二个权值,也可以先构造一个最短路子图,求其中的最短路树,也就是求最小树形图。(注意这里不是Kruskal可以解决的,,,因为其实这个新图加的是有向边、、、)

比如这个样例:

不难看出,最短路子图应该是这样的:

而没有由上到下的那条边。

 

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct Edge {int fr,to;ll w,c;int ne;
} e[MAX];
struct Graph {struct Point {int pos;ll c;Point() {}Point(int pos,ll c):pos(pos),c(c) {}bool operator<(const Point b) const {return c > b.c;}};Edge e[MAX];int tot = 0;//总共tot条边,编号0~(tot-1)int head[MAX];bool vis[MAX];ll d[MAX],cost[MAX];void add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void Dijkstra(int st) {priority_queue<Point> pq;memset(d,0x3f,sizeof d);memset(cost,0x3f,sizeof cost);memset(vis,0,sizeof vis);d[st] = 0;cost[st] = 0;pq.push(Point(st,0));while(pq.size()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;for(int i = head[cur.pos]; ~i; i = e[i].ne) {if(d[e[i].to] > d[cur.pos] + e[i].w) {d[e[i].to] = d[cur.pos] + e[i].w;cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}else if(d[e[i].to] == d[cur.pos]+e[i].w) {if(cost[e[i].to] > e[i].c) {cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}}}}}void id_add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void init(int n) {tot = 0;for(int i = 0; i<=n; i++) head[i] = -1;}
} G1;
int main() {int n,m;int u,v;ll w,c;int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);G1.init(n);for(int i = 1; i<=m; i++) {scanf("%d%d%lld%lld",&u,&v,&w,&c);u++,v++;G1.add(u,v,w,c);G1.add(v,u,w,c); }G1.Dijkstra(1);ll ans1 = 0,ans2 = 0;for(int i = 1; i<=n; i++) ans1 += G1.d[i],ans2 += G1.cost[i];printf("%lld %lld\n",ans1,ans2);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440442.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle 主键约束复制,Oracle主键及约束

Oracle主键Primary Key包含非空约束及唯一约束。添加主键的语句alter table table_nameadd constraint cons_name primary key(col_name);查看主键是否被创建成功select dbms_metadata.get_ddl(‘OBJECT_TYPE‘,‘NAME‘,‘SCHEMA‘) from dual;OBJECT_TYPE (TABLE,PARTITION,I…

【ZOJ - 3956】Course Selection System(01背包)

题干&#xff1a; There are n courses in the course selection system of Marjar University. The i-th course is described by two values: happiness Hi and credit Ci. If a student selects m courses x1, x2, ..., xm, then his comfort level of the semester can be…

oracle 账户 锁定 密码忘记了,Oracle System密码忘记 密码修改、删除账号锁定lock

运行cmd命令行录入 sqlplus /nolog 无用户名登录conn /as sysdba 连接到数据本地数据alter user system identified by password; 修改System 密码 为passwordD:\oracle\ora92\bin>sqlplus /nologSQL*Plus: Release 9.2.0.1.0 - Production on 星期四 8月 16 11:32:22 …

【计蒜客 - 2019南昌邀请赛网络赛 - K】MORE XOR(数学,找规律,打表)

Given a sequence of nn numbers a_1, a_2, \cdots, a_na1​,a2​,⋯,an​ and three functions. Define a function f(l,r)f(l,r) which returns \oplus a[x]⊕a[x] (l \le x \le rl≤x≤r). The \oplus⊕ represents exclusive OR. Define a function g(l,r)g(l,r) which r…

oracle编译失效物化视图,使用“不存在”的Oracle物化视图

启用快速刷新很棘手,有许多奇怪的限制和无用的错误消息.在这种情况下,您需要创建物理化视图日志WITH ROWID,使用()连接语法,并为每个表添加ROWID.create table tablea(my_id number primary key,a number);create table tableb(my_id number primary key,b number);create mate…

【计蒜客 - 2019南昌邀请赛网络赛 - M】Subsequence(字典树,dp预处理)

题干&#xff1a; Give a string SS and NN string T_iTi​ , determine whether T_iTi​ is a subsequence of SS. If ti is subsequence of SS, print YES,else print NO. If there is an array \lbrace K_1, K_2, K_3,\cdots, K_m \rbrace{K1​,K2​,K3​,⋯,Km​} so th…

Linux把文件移动到容器外,Docker容器与主机之间拷贝文件的方法

一般情况下&#xff0c;我们在启动Docker容器的时候可以使用-v参数映射宿主机的文件或者目录到容器里&#xff0c;这样的话&#xff0c;在宿主机相关目录下的文件修改会自动在容器里生效。但是&#xff0c;如果我们已经启动了一个容器的话&#xff0c;就只能使用下面的这种方式…

【计蒜客 - 2019南昌邀请赛网络赛 - H】Coloring Game(找规律,思维dp)

题干&#xff1a; David has a white board with 2 \times N2N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately. Each time he can mov…

linux打包tar包命令,Linux压缩打包方法连载之一:tar命令

Linux压缩打包方法有很多种&#xff0c;以下讲了tar命令的概念&#xff0c;同时文列举了多种范例供大家查看&#xff0c;希望对大家有所帮助....tar命令[rootlinux ~]# tar [-cxtzjvfpPN] 文件与目录 ....参数&#xff1a;-c &#xff1a;建立一个压缩文件的参数指令(create 的…

【计蒜客 - 2019南昌邀请赛网络赛 - I】Max answer(单调栈,RMQ)

题干&#xff1a; Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval. Now she is planning to find the max value of the intervals in her arr…

linux联想电脑wifi密码,联想笔记本Y7000—ubuntu16.4无法开启wifi的解决办法

一、问题描述本人使用的是联想游戏本Y7000&#xff0c;默认装入Win10系统&#xff0c;然当装入Ubuntu16.04双系统时&#xff0c;会出现无线硬件开关关闭的问题&#xff0c;当然也就无法连网。使用rfkill list all会出现如下提示0:ideapad_wlan: Wireless LANSoft blocked: noHa…

【HDU - 6514】Monitor(二维差分,前缀和)

题干&#xff1a; Monitor Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 872 Accepted Submission(s): 145 Problem Description Xiaoteng has a large area of land for growing crops, and the land…

linux网卡限速tc,linux tc 对本机网卡限速

今天由于项目要测试在网络环境不好的情况下&#xff0c;会不会对平台有所影响&#xff0c;要求测试限制服务器流量&#xff0c;我想到了TC&#xff0c;我对TC不是很了解&#xff0c; google了一下&#xff0c;看到了不少文章都是做路由用的&#xff0c;对我来说不是很实用&…

【ZOJ - 3715】Kindergarten Election(枚举得票数,贪心)

题干&#xff1a; At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader. The ith kid will vote for his best friend fi (where 1 ≤ fi ≤ n, and its too shame to vo…

修改DNS的Linux脚本,Shell脚本-配置网络

原先学习过shll但是只是了解一下它的语法而已&#xff0c;在平时中并没有使用&#xff0c;在暑假的时候又想起了shell&#xff0c;所以又回顾了一下&#xff0c;学习后一定要用&#xff0c;这样才能掌握。最近实验室刚装好机子&#xff0c;什么都要配置一下&#xff0c;包括网络…

【ZOJ - 3872】Beauty of Array(思维,计算贡献,枚举)

题干&#xff1a; Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A. Input There…

linux 使用gzip压缩打包的文件,linux常用的解压,压缩,打包

gzip zcat[rootlinux ~]# gzip [-cdt#] 文件名[rootlinux ~]# zcat 文件名.gz参数&#xff1a;-c : 将压缩的数据输出到屏幕上&#xff0c;可通过数据流重导向来处理-d &#xff1a;解压缩参数-t &#xff1a; 可以用来检验一个压缩文件的一致性&#xff5e;看看文件有无错误-#…

【POJ - 1836】Alignment(dp,LIS,最长上升子序列类问题)

题干&#xff1a; In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the …

Windows共享Linux打印机,linux – 如何为cups客户端构建windows共享打印机的url

从this page开始&#xff1a;smbThis backend sends print files to printers shared by a Windows host. Examplesof CUPS device-URIs that may be used includes:smb://workgroup/server/printersharenamesmb://server/printersharenamesmb://username:passwordworkgroup/se…

【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)

题干&#xff1a; 给你两个DNA序列&#xff08;长度不一定相同&#xff09;&#xff0c;你可以在其中任意位置上加入空格&#xff0c;使得最终他俩长度相同&#xff0c;最终相同长度的两个DNA序列会有个相似度比较&#xff08;每个字符相对应的比较&#xff09;&#xff0c;问…