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 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).
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
题意:从0点开始到各点距离最小,在距离最小情况下还要花费最少,做这题的时候题目没搞明白,
优先队列维护,
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <cstring> 6 using namespace std; 7 typedef long long LL; 8 const LL INF = ((LL) 1) << 61; 9 const int Max = 1e5 + 10; 10 struct Edge 11 { 12 int v, d, c; 13 int Next; 14 }; 15 struct Node 16 { 17 int u; 18 LL d, c; 19 friend bool operator<(const Node & a, const Node & b) 20 { 21 if (a.d == b.d) 22 return a.c > b.c; 23 return a.d > b.d; 24 } 25 }; 26 Node node[Max]; 27 Edge edge[Max * 2]; 28 int head[Max]; 29 int vis[Max]; 30 int n, m, tot; 31 void addEdge(int u, int v, int d, int c) 32 { 33 edge[tot].v = v; edge[tot].d = d; edge[tot].c = c; 34 edge[tot].Next = head[u]; 35 head[u] = tot++; 36 37 edge[tot].v = u; edge[tot].d = d; edge[tot].c = c; 38 edge[tot].Next = head[v]; 39 head[v] = tot++; 40 } 41 42 void dijstra() 43 { 44 for (int i = 0; i <= n; i++) 45 { 46 node[i].u = i; 47 node[i].d = INF; 48 node[i].c = INF; 49 } 50 priority_queue<Node> que; 51 node[0].c = node[0].d = 0; 52 que.push(node[0]); 53 memset(vis, 0, sizeof(vis)); 54 55 while (!que.empty()) 56 { 57 Node temp = que.top(); 58 que.pop(); 59 int u = temp.u; 60 if (vis[u]) 61 continue; 62 vis[u] = 1; 63 for (int i = head[u]; i != -1; i = edge[i].Next) 64 { 65 if (vis[ edge[i].v ]) 66 continue; 67 if (node[ edge[i].v ].d > node[ u ].d + edge[i].d) 68 { 69 node[ edge[i].v ].d = node[ u ].d + edge[i].d; 70 node[ edge[i].v ].c = edge[i].c; 71 que.push(node[ edge[i].v ]); 72 } 73 else if (node[ edge[i].v ].d == node[ u ].d + edge[i].d && node[ edge[i].v ].c > edge[i].c) 74 { 75 node[ edge[i].v ].c = edge[i].c; 76 que.push(node[ edge[i].v ]); 77 } 78 } 79 } 80 } 81 int main() 82 { 83 int t; 84 scanf("%d", &t); 85 while (t--) 86 { 87 scanf("%d%d", &n, &m); 88 memset(head, -1, sizeof(head)); 89 int u, v, d, c; 90 tot = 0; 91 for (int i = 1; i <= m; i++) 92 { 93 scanf("%d%d%d%d", &u, &v, &d, &c); 94 addEdge(u, v, d, c); 95 } 96 dijstra(); 97 LL sum_d = 0, sum_c = 0; 98 for (int i = 1; i < n; i++) 99 { 100 sum_d += node[i].d; 101 sum_c += node[i].c; 102 } 103 printf("%lld %lld\n", sum_d, sum_c); 104 } 105 return 0; 106 }