ZOJ Problem Set - 3946 Highway Project
来源:互联网 发布:淮南市大数据 编辑:程序博客网 时间:2024/06/05 20:57
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 toN - 1 (the capital is 0) and there are M highways can be built. Building thei-th highway costs Ci dollars. It takes Di minutes to travel between cityXi 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 cityi (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
24 50 3 1 10 1 1 10 2 10 102 1 1 12 3 1 24 50 3 1 10 1 1 10 2 10 102 1 2 12 3 1 2
Sample Output
4 34 4一道裸的spfa。做题的时候没有将cost和i赋值为足够大,一直wa,烦死了#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>using namespace std;typedef long long LL;const int N = 50005 * 5;const LL INF = 100000000000;struct Node{LL v, c, d;int next;}node[N];int head[N];int pos = 0;LL cost[N], T[N];bool inq[N];void init(){pos = 0;fill(cost, cost + N, INF);fill(T, T + N, INF);memset(inq, 0, sizeof(inq));memset(head, -1, sizeof(head));}void addEdge(int u, int v, int t, int c){//前向星node[pos].v = v;node[pos].c = c;node[pos].d = t;node[pos].next = head[u];head[u] = pos++;}void SPFA(){queue<int> q;q.push(0);inq[0] = 1;cost[0] = 0;T[0] = 0;while (!q.empty()){int v = q.front();//循环到节点vq.pop();inq[v] = 0;for (int i = head[v]; i + 1; i = node[i].next){int pos = node[i].v;//城市v能到达的城市if (T[v] + node[i].d <= T[pos]){if (T[v] + node[i].d < T[pos]){T[pos] = T[v] + node[i].d;cost[pos] = node[i].c;if (!inq[pos]){//城市pos是否在队列中inq[pos] = 1;q.push(pos);}}else if (cost[pos]>node[i].c){cost[pos] = node[i].c;if (!inq[pos]){inq[pos] = 1;q.push(pos);}}}}}}int main(){int t;#ifdef glxfreopen("in.txt", "r", stdin);#endifscanf("%d", &t);while (t--){int n, m;scanf("%d%d", &n, &m);int u, v, c, d;init();for (int i = 0; i < m; i++){scanf("%d%d%d%d", &u, &v, &d, &c);addEdge(u, v, d, c);addEdge(v, u, d, c);}SPFA();LL dd = 0, cc = 0;for (int i = 0; i < n; i++){dd += T[i];cc += cost[i];}printf("%lld %lld\n", dd, cc);}return 0;}
- ZOJ Problem Set - 3946 Highway Project
- ZOJ Problem Set - 3946 Highway Project
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project(Dijkstra)
- ZOJ 3946 Highway Project (spfa)
- ZOJ 3946 Highway Project 单源最短路
- ZOJ-3946-Highway Project(最短路)
- ZOJ 3946 Highway Project【dijkstra】【贪心】
- zoj 3946 Highway Project(最短路径)
- zoj--3946--Highway Project(SPFA)
- ZOJ 3946 Highway Project 优先队列dijk
- ZOJ 3946 Highway Project(最短路)
- [最短路应用] ZOJ 3946 Highway Project
- ZOJ Problem Set - 1423 (Your)((Term)((Project)))
- 正则表达式知识详解之单词边界 (java版示例)
- 欢迎使用CSDN-markdown编辑器
- 设计模式之基本原则
- PAT-B 1005. 继续(3n+1)猜想
- 289. Game of Life
- ZOJ Problem Set - 3946 Highway Project
- 在单据维护界面仅下推选中的单据体行
- C# 接口详解
- Linux程序前台后台切换
- 68-类的成员变量
- 文章标题
- 付款申请单,如何追查跨级源头采购订单
- 推排序举例
- (水)POJ-2376 区间贪心,区间覆盖