Aizu 2249 Road Construction【最短路变形】
来源:互联网 发布:golang java性能对比 编辑:程序博客网 时间:2024/05/16 11:03
Road Construction
Time Limit : 8 sec, Memory Limit : 65536 KBProblem H: Road Construction
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected.
In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions:
- For every pair of cities, there is a route (a set of roads) connecting them.
- The minimum distance between the capital and each city does not change from his original plan.
Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost.
Input
The input consists of several datasets. Each dataset is formatted as follows.
N M
u1 v1 d1 c1
.
.
.
uM vM dM cM
The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and Mindicate the number of cities and the number of roads in the original plan, respectively.
The following M lines describe the road information in the original plan. The i-th line contains four integers, ui,vi, di and ci (1 ≤ ui, vi ≤ N , ui ≠ vi , 1 ≤ di ≤ 1000, 1 ≤ ci ≤ 1000). ui , vi, di and ci indicate that there is a road which connects ui-th city and vi-th city, whose length is di and whose cost needed for construction is ci.
Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom.
The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset.
Output
For each dataset, print the minimum cost of a plan which satisfies the conditions in a line.
Sample Input
3 31 2 1 22 3 2 13 1 3 25 51 2 2 22 3 1 11 4 1 14 5 1 15 3 1 15 101 2 32 101 3 43 431 4 12 521 5 84 232 3 58 422 4 86 992 5 57 833 4 11 323 5 75 214 5 23 435 101 2 1 531 3 1 651 4 1 241 5 1 762 3 1 192 4 1 462 5 1 253 4 1 133 5 1 654 5 1 340 0
Output for the Sample Input
35137218
题意:
给出若干个建筑之间的一些路,每条路都有对应的长度和需要的花费,问在保证源点1 到其他个点的距离最短的情况下,最少的花费是多少
题解:
首先明确需要使用邻接表存图,解题的第一个想法就是最短路上套一个最小生成树,但是相对会比较复杂,后来想,在更新最短路的时候,同时进行最小花费的更新,应该是行得通的,毕竟花费想当于是二级动态更新的属性,效果应该是一样的,然后上手做了.....
因个人对最短路其实并不是很了解,最终实现的时候程序存在漏洞,错了好几次才AC....
网上的题解都是用spfa 做的,说是这道题考的就是spfa ,个人不是太理解,spfs 和dijkstra 在处理不存在负权值的边时的更新操作难道不是一样的,都是进行松弛操作?
然后硬是用dijkstra 做了....
/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const int maxn=10005;int edgenum,head[maxn],dist[maxn];struct node{int from,to,len,val;int next;}edge[maxn*5];void init(int n){edgenum=0;memset(head,-1,sizeof(head));memset(dist,inf,sizeof(dist));}int add(int u,int v,int d,int c){node tp={u,v,d,c,head[u]};edge[edgenum]=tp;head[u]=edgenum++;}int dijkstra(int n,int st){int vis[maxn]={0},num[maxn];memset(num,-1,sizeof(num));//num 保存最后更新某个点的最短路使用了哪条边dist[st]=0;while(1){int v=-1;for(int u=1;u<=n;++u){if(!vis[u]&&(v==-1||dist[u]<dist[v])){v=u;}}if(v==-1){break;}vis[v]=1;for(int i=head[v];i!=-1;i=edge[i].next){int u=edge[i].to;if(dist[u]>dist[v]+edge[i].len){dist[u]=dist[v]+edge[i].len;num[u]=i;}else if(dist[u]==dist[v]+edge[i].len){if(num[u]!=-1&&edge[num[u]].val>edge[i].val)//考虑能否减少花费{num[u]=i;}}}}int ans=0;for(int i=1;i<=n;++i){if(num[i]!=-1){ans+=edge[num[i]].val;}}return ans;}int main(){int n,m;//freopen("shuju.txt","r",stdin);while(scanf("%d%d",&n,&m),n|m){init(n);for(int i=0;i<m;++i){int u,v,d,c;scanf("%d%d%d%d",&u,&v,&d,&c);add(u,v,d,c);add(v,u,d,c);}printf("%d\n",dijkstra(n,1));}return 0;}
- Aizu 2249 Road Construction【最短路变形】
- Aizu 2249Road Construction 单源最短路变形 spfa模板改写
- Aizu 2249 Road Construction
- AIZU 2249 Road Construction
- Aizu 2249Road Construction
- Aizu 2249Road Construction(SPFA算法变形,好题)
- AOJ 2249 Road Construction 最短路
- Aizu 2249 Road Construction(SPFA)
- Road Construction Aizu
- AOJ 2249 Road Construction (dijkstra最短路)
- AOJ 2249 Road Construction(双权值最短路)
- Aizu 2249 单源最短路变形 spfa模板改写
- HDU 1596find the safest road(最短路变形)
- HDOJ find the safest road 1596【最短路变形】
- HDU 1596 find the safest road(最短路变形)
- HDU1596 find the safest road(最短路变形,Dijkstra)
- 3040: 最短路(road)
- 3040: 最短路(road)
- SD卡的特点和背景知识
- 关于使用Hibernate注解的时候遇到的问题
- Libnids详解
- Xcode使用介绍之六:常用快捷键
- Direct2D Intro - Flip and Rotate a Bitmap Source
- Aizu 2249 Road Construction【最短路变形】
- DeviceInfo-设备信息
- $_GET是数组
- mapreduce原理分析
- Mac OS 搭建 Jenkins 实践(Tomcat + jenkins.war)
- HM编码器代码阅读(7)——整个编码流程以及相关的函数
- 对一致性Hash算法,java代码实现的深入研究
- leetcode——5——Longest Palindromic Substring
- 浅谈Java的反射机制