zoj 3946 Highway Project【SPFA多个性质的最优化】
2016-04-25 21:53
423 查看
Highway Project
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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.
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).
Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
Submit
Status
代码:(注意:用SPFA时边的数组开成边的个数的最大值的2倍)
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 integerT 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
Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
Submit
Status
代码:(注意:用SPFA时边的数组开成边的个数的最大值的2倍)
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define LL long long #define INF 0x3f3f3f3f using namespace std; int n,m; struct Edge { LL from,to,val,time,next; }edge[200005];//双向的边,所以应该是边的总数乘以2!!!wa哭我了!!! LL head[100005]; LL vis[100005]; LL dis[100005]; LL tm[100005]; LL edgenum; void addEdge(LL x,LL y,LL z,LL t) { Edge E={x,y,z,t,head[x]}; edge[edgenum]=E; head[x]=edgenum++; } void SPFA() { queue<LL>q; q.push(0); vis[0]=1;//进队列的要进行标记 dis[0]=0; tm[0]=0; while(!q.empty()) { LL u=q.front(); q.pop(); vis[u]=0;//出来的要取消标记! for(LL i=head[u];i!=-1;i=edge[i].next) { LL v=edge[i].to; if(tm[v]>tm[u]+edge[i].time) { tm[v]=tm[u]+edge[i].time; dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了! if(!vis[v])//没进队列的将它放进队列,用于更新它周围的点 { vis[v]=1; q.push(v); } } else if(tm[v]==tm[u]+edge[i].time) { if(dis[v]>edge[i].val) { dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了! } } } } } int main() { LL T; scanf("%lld",&T); while(T--) { edgenum=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(LL i=0;i<m;i++) { LL x,y,z,t; scanf("%lld%lld%lld%lld",&x,&y,&t,&z); addEdge(x,y,z,t); addEdge(y,x,z,t); } memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); memset(tm,INF,sizeof(tm)); SPFA(); LL s1=0,s2=0; for(int i=1;i<n;i++) { s1+=tm[i]; s2+=dis[i]; } printf("%lld %lld\n",s1,s2); } return 0; }
相关文章推荐
- Netty in action thrid chapter 心得总结
- UVA_12210_A Match Making Problem
- iOS ----新特性 3DTouch 开发教程全解(含源码)
- JS+CSS3人物奔跑动画
- 团队项目:个人工作总结07
- ServletContext在tomcat启动的时候创建
- SQL 日期格式化
- Android5.0的RecycleView技术
- 软件过程模型(生命周期模型)
- 面试题1
- 【定时任务】JDK java.util.Timer定时器的实现原理
- 毛玻璃效果随着tableView滑动清晰
- ACdream 1157 Segments 【CDQ分治】
- 每周总结
- 第一冲刺阶段站立会议09
- (java)求最长递增子序列(可以不连续的情况)
- Java IO系统——File类
- 分组选择符
- 第四章Android移植环境搭建
- andorid 用户界面布局学习心得