zoj 3946 Highway Project【SPFA多个性质的最优化】
来源:互联网 发布:姚明05-06赛季数据 编辑:程序博客网 时间:2024/06/06 00:17
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 <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define LL long long#define INF 0x3f3f3f3fusing 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;}
- zoj 3946 Highway Project【SPFA多个性质的最优化】
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project (spfa)
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- zoj--3946--Highway Project(SPFA)
- ZOJ-3946-Highway Project(最短路)
- zoj 3946 Highway Project(最短路径)
- ZOJ 3946 Highway Project(最短路)
- [最短路应用] ZOJ 3946 Highway Project
- 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【13th浙江省赛】【最短路】
- 优化Duilib图片属性解析代码,提升绘制效率
- 分组选择符
- andorid 用户界面布局学习心得
- 面试题1
- 数据库操作工具类
- zoj 3946 Highway Project【SPFA多个性质的最优化】
- Dubbo学习(十一):结果缓存
- Android5.0的RecycleView技术
- SQL 日期格式化
- JAVA可视化开发工具:jigloo
- iOS ----新特性 3DTouch 开发教程全解(含源码)
- 参数修饰符
- 连接池
- linux基本命令(2)——cd命令