ZOJ 3946 Highway Project 单源最短路
来源:互联网 发布:卡密自动发货源码 编辑:程序博客网 时间:2024/06/05 21:08
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
Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<iterator>#include<stack>using namespace std;typedef long long ll;const int INF=1e9+7;const ll inf=1e15;const double eps=1e-7;void g(){ puts("Hello"); }int n,m;const int maxn=100000;struct Edge{ int from,to; ll w,cost; Edge(){} Edge(int from,int to,ll w,ll cost):from(from),to(to),w(w),cost(cost){}};vector<int >G[maxn+10];vector<Edge>edges;void add_edge(int x,int y,ll w,ll cost){ edges.push_back(Edge(x,y,w,cost)); edges.push_back(Edge(y,x,w,cost)); int m=edges.size(); G[x].push_back(m-2); G[y].push_back(m-1);}void init()//初始化{ edges.clear(); for(int i=0;i<=n;i++) { G[i].clear(); }}bool done[maxn+10];struct Node//dijkstra优先队列的堆元素{ int ind; ll w,cost; Node(){} Node(int ind,ll w,ll cost):ind(ind),w(w),cost(cost){} bool operator<(const Node y)const { return w>y.w; }};ll d[maxn+10],Cos[maxn+10];void dijkstra()//dijkstra{ memset(done,0,(n+1)*sizeof done[0]);// memset(d,0x3f,(n+1)*sizeof d[0]); for(int i=0;i<=n;i++) { d[i]=inf; Cos[i]=inf;//Cos[x]记录的是到x花费最小的一条边的花费,不能表示从源点到x修路的最小花费。 } d[0]=0; Cos[0]=0; priority_queue<Node>q; q.push(Node(0,0,0)); while(!q.empty()) { Node now=q.top();q.pop(); int x=now.ind;// cout<<x<<endl; if(done[x]) continue; done[x]=1; for(int i=0;i<G[x].size();i++) { int id=G[x][i]; Edge &e=edges[id]; int y=e.to;// if(done[y]) continue; ll w=e.w; ll cost=e.cost; if(d[x]+w<d[y]) { d[y]=d[x]+w; Cos[y]=cost; q.push(Node(y,d[y],Cos[y]) ); } else if(d[x]+w==d[y]&&cost<Cos[y]) { Cos[y]=cost; q.push(Node(y,d[y],Cos[y])); } } } ll aw=0,ac=0; for(int i=1;i<n;i++) { aw+=d[i]; ac+=Cos[i]; } printf("%lld %lld\n",aw,ac);}int main(){ int T; scanf("%d",&T); int x,y; ll w,cost; while(T--) { scanf("%d%d",&n,&m); init(); for(int i=1;i<=m;i++) { scanf("%d%d%lld%lld",&x,&y,&w,&cost); add_edge(x,y,w,cost); } dijkstra(); } return 0;}
- ZOJ 3946 Highway Project 单源最短路
- ZOJ-3946-Highway Project(最短路)
- ZOJ 3946 Highway Project(最短路)
- [最短路应用] ZOJ 3946 Highway Project
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- zoj-3946-Highway Project【13th浙江省赛】【最短路】
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- zoj 3946Highway Project(最短路改)
- ZOJ 3946 Highway Project(多属性边权最短路)
- ZJOJ Highway Project 3946(单源最短路+贪心)
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project(Dijkstra)
- ZOJ 3946 Highway Project (spfa)
- ZOJ 3946-Highway Project【最短路的应用】(2016浙江省大学生程序设计竞赛)
- ZOJ 3946 Highway Project(最小费用最短路,优先队列优化dijkstra)
- HBase 数据库检索性能优化策略
- 软件设计之模块划分
- ubuntu 14.04配置python安装第三方库(numpy,scipy,matplotlib等)
- CodeForces 600E Lomsat gelral(树形dp+启发式合并)
- 浅谈个人对互联网未来发展的见解
- ZOJ 3946 Highway Project 单源最短路
- 如何保存grep 结果里面的换行符?
- Shell之网站日志的PV,UV计算
- iOS开发系列--地图与定位
- Android使用NotificationManager来进行任务栏通知(使用Bmob第三方推送)
- 基于NI9237模块实现应变检测
- HDU 1205 吃糖果(数学题)
- ZOJ 3943Himalayas
- Java Web连接各种数据库方式汇总