aoj2249(最短路)
来源:互联网 发布:条形码生成器软件下载 编辑:程序博客网 时间:2024/06/14 01:52
Description
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
u1v1d1c1
.
.
.
uMvMdMcM
The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and M indicate 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
在最短路的基础上求最小花费。不过spfa的过程里面有个令人纠结的问题是如何去计算最小花费,因为如果是模拟计算最短路的方式再开一个数组记录最小花费的话,其实是不对的,因为那是计算的从1到每个点的最短花费,并不是整个工程的最小花费,会有重复部分。所以无比纠结。。后来想想,从点1到各个点的最短路一定会形成一个树,所以每个点其实只有一个点指向它,所以我开了一个jl(记录)数组来记录每个点被指向的直接花费,其实比较好理解。
#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;typedef long long ll;const int maxn=10010,INF=99999999;int ip,n,m,head[maxn],dis[maxn],jl[maxn];bool vis[maxn];struct data{ int next,to,d,w;} tu[maxn*maxn];void init(){ ip=0; memset(head,-1,sizeof(head));}void add(int a,int b,int d,int w){ tu[ip].to=b,tu[ip].d=d,tu[ip].w=w,tu[ip].next=head[a],head[a]=ip++;}void spfa(){ for(int i=1; i<=n; i++) dis[i]=jl[i]=INF; dis[1]=0; jl[1]=0; memset(vis,0,sizeof(vis)); queue<int>q; q.push(1); while(!q.empty()) { int t=q.front(); q.pop(); vis[t]=0; for(int k=head[t]; k!=-1; k=tu[k].next) { int d=tu[k].d,v=tu[k].to,w=tu[k].w; if(dis[t]+d<dis[v]) { jl[v]=w;///记录最小花费 dis[v]=dis[t]+d; if(!vis[v]) { vis[v]=1; q.push(v); } } else if(dis[t]+d==dis[v]&&w<jl[v])///记录最小花费 jl[v]=w; } } int ans=0; for(int i=1; i<=n; i++) ans+=jl[i]; printf("%d\n",ans);}int main(){ while(~scanf("%d%d",&n,&m)&&m+n) { init(); while(m--) { int a,b,d,w; scanf("%d%d%d%d",&a,&b,&d,&w); add(a,b,d,w); add(b,a,d,w); } spfa(); } return 0;}
- aoj2249(最短路)
- aoj2249(dijkstra最短路)
- 最短路(最短路)
- HDOJ2544 最短路(最短路)
- 最短路 (Dijstra) 最短路
- HDU2544:最短路(最短路)
- hdu2544(最短路)
- 随笔(最短路)
- hdu3790 (最短路)
- 最短路(dijkstra)
- 最短路--(迪杰斯特拉)
- 最短路(dijkstra)
- poj1535(最短路)
- HLJOJ1214(最短路)
- 最短路(Dijkstra)
- 最短路(floyd)
- 最短路(Bellman_Ford)
- 最短路(SPFA)
- Centos6.5安装Nginx的教程 先安装nginx依赖的库pcre
- httpd+nfs 简单的文件系统共享
- python的time和datetime模块 之 时间形式互转
- servlet 一个simple web project
- Greenland'sMeltwater May Fertilize Fjords with Phosphorus
- aoj2249(最短路)
- node 学习的一些资料收集
- 重构--第一个案例
- 朴素贝叶斯
- 你所不知道的黑客工具之 EK 篇
- 深入理解Android的startservice和bindservice
- spring中使用Quartz任务调度框架
- SharePoint2013 IT Professional - Version Setting
- CORBA Programming with TAO - 1.Start(基本概念)