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.
There are multiple test cases. The first line of input contains an integer T indicating 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).
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.
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
4 3 4 4Author: Lu, Yi
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } #define MS(x,y) memset(x,y,sizeof(x)) #define MC(x,y) memcpy(x,y,sizeof(x)) #define MP(x,y) make_pair(x,y) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 1e5+10, M = 2e5+10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; int casenum, casei; int n, m; struct node { int x; LL dis; node(int x, LL dis) :x(x), dis(dis) {}; bool operator < (const node&b)const { return dis > b.dis; } }; int first[N], id; int w[M], dis[M], cost[M], nxt[M]; void ins(int x, int y, int d, int c) { w[++id] = y; dis[id] = d; cost[id] = c; nxt[id] = first[x]; first[x] = id; } bool e[N]; LL f[N]; int pre[N]; priority_queue<node>q; void inq(int x, LL d, int p) { if (d > f[x])return; if (d == f[x]) { gmin(pre[x], p); return; } f[x] = d; pre[x] = p; q.push(node(x, d)); } void dijkstra() { memset(f, 63, n << 3); memset(pre, 63, n << 2); memset(e, 0, n); inq(0, 0, 0); while (!q.empty()) { int x = q.top().x; q.pop(); if (e[x])continue; else e[x] = 1; for (int z = first[x]; z; z = nxt[z])inq(w[z], f[x] + dis[z], cost[z]); } } int main() { scanf("%d", &casenum); for (casei = 1; casei <= casenum; ++casei) { scanf("%d%d", &n, &m); memset(first, 0, n << 2); id = 1; for (int i = 1; i <= m; ++i) { int x, y, d, c; scanf("%d%d%d%d", &x, &y, &d, &c); ins(x, y, d, c); ins(y, x, d, c); } dijkstra(); LL totdis = 0; LL totcost = 0; for (int i = 1; i < n; ++i) { totdis += f[i]; totcost += pre[i]; } printf("%lld %lld\n", totdis, totcost); } return 0; } /* 【trick&&吐槽】 贪心就是力量! 【题意】 有n(1e5)个点,m(1e5)条双向边。 0base 对于每条边,有(x,y,dis,cost) 我们希望使得—— 1,选择(0~m)条边,在选择的这些边的连通性条件下,使得—— 2,0号点到其他所有点的距离dis之和(即0号点到其他所有点的最短路之和)尽可能小, 3,在满足1,2的条件下,我们所选的边的费用成本cost之和尽可能小。 【类型】 最小树形图 or 贪心 【分析】 首先,我们所选的边,显然一定都是来源于最短路上的边 在这个条件下,如果我们以0号点为源点,以最短路上的边为待选边,做一个最小树形图,显然就能得到答案。 然而,不会nlogn的最小树形图算法,我们该怎么办? 最小生成树算法是错误的 我们只要使得每条边的前驱边是所有可选前驱边中边权最小的即可。 【时间复杂度&&优化】 O(nlogn) */