ZOJ 3946 Highway Project (spfa)
来源:互联网 发布:sass for mac中文版 编辑:程序博客网 时间:2024/05/16 11:55
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718
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~n-1个城市,然后可以修M条路。走完每条路要Di的时间,修这条路要Ci的花费。
然后让你修一些路使其满足从0到其他每个城市的总的时间花费最短。在这个条件满足的情况下下,找总的最小的花费。
解法: 依题意得,求每个点到0这个点的最短路径,明显spfa。
利用贪心的思想,取每个点的入边的最小花费,相加可得答案。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int M = 1e5 + 100;const long long INF = 0x3f3f3f3f;typedef long long ll;struct Node{ int next,to; ll d,c; Node(){} Node(int a,int b,ll d,ll c):next(a),to(b),d(d),c(c){}}Edge[M*2];int head[M]; int nedges; ll dis[M];ll in[M],vis[M];void add_edge(int a,int b,ll c,ll d){ Edge[++nedges] = Node(head[a],b,c,d); head[a] = nedges; Edge[++nedges] = Node(head[b],a,c,d); head[b] = nedges;}void spfa(int n){ queue<int>que; dis[0] = 0; que.push(0); vis[0] = true; while(!que.empty()) { int now = que.front(); que.pop(); vis[now] = false; for(int i = head[now];~i;i=Edge[i].next) { int v = Edge[i].to; if(dis[now] + Edge[i].d <= dis[v]) { if(dis[now] + Edge[i].d == dis[v]) in[v] = min(in[v],Edge[i].c); else in[v] = Edge[i].c; dis[v] = dis[now] + Edge[i].d; if(!vis[v]) { que.push(v); vis[v] = true; } } } } ll nowl = 0,nowr = 0; for(int i=1;i<n;i++) nowl += dis[i],nowr += in[i]; printf("%lld %lld\n",nowl,nowr);}int main(){ int T; scanf("%d",&T); while(T--) { int n,m; nedges = -1; memset(head,-1,sizeof(head)); memset(dis,INF,sizeof(dis)); memset(vis,false,sizeof(vis)); memset(in,INF,sizeof(in)); scanf("%d%d",&n,&m); while(m--) { int x,y; ll d,c; scanf("%d%d %lld%lld",&x,&y,&d,&c); add_edge(x,y,d,c); } spfa(n); } return 0;}
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project (spfa)
- zoj--3946--Highway Project(SPFA)
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- zoj 3946 Highway Project (spfa + 最小生成树)
- ZOJ 3946 Highway Project SPFA 两个限制条件
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ--Highway Project(spfa+两级选择)
- ZOJ 3946 Highway Project(Dijkstra)
- zoj 3946 Highway Project【SPFA多个性质的最优化】
- ZOJ 3946 Highway Project 单源最短路
- ZOJ-3946-Highway Project(最短路)
- ZOJ Problem Set - 3946 Highway Project
- ZOJ 3946 Highway Project【dijkstra】【贪心】
- zoj 3946 Highway Project(最短路径)
- Orcal数据库的表结构转换成mysql数据库的表结构
- IOS在后台每隔一段时间执行一下 资料1
- 在非主线程是否可以更新UI
- poj-1207-The 3n + 1 problem
- YK线上机器cpu、内存信息
- ZOJ 3946 Highway Project (spfa)
- http通信
- HTML基础知识总结
- KMP算法
- mySQL
- IOS在后台每隔一段时间执行一下 资料2
- 时间复杂度与空间复杂度
- bzoj 1176: [Balkan2007]Mokia 【CDQ分治】
- hdoj-5590-ZYB's Biology