我是靠谱客的博主 拼搏秋天,这篇文章主要介绍hdu1874 畅通project续 最短路 floyd或dijkstra或spfa,现在分享给大家,希望可以做个参考。

Problem Description
某省自从实行了非常多年的畅通project计划后。最终修建了非常多路。只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择。而某些方案要比还有一些方案行走的距离要短非常多。这让行人非常困扰。

如今,已知起点和终点。请你计算出要从起点到终点,最短须要行走多少距离。

Input
本题目包括多组数据,请处理到文件结束。
每组数据第一行包括两个正整数N和M(0< N<200,0 < M<1000),分别代表现有城镇的数目和已修建的道路的数目。

城镇分别以0~N-1编号。


接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B < N,A!=B,0< X <10000),表示城镇A和城镇B之间有一条长度为X的双向道路。


再接下一行有两个整数S,T(0<=S,T < N),分别代表起点和终点。

Output
对于每组数据,请在一行里输出最短须要行走的距离。假设不存在从S到T的路线,就输出-1.

Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output
2
-1

Author
linle

Source
2008浙大研究生复试热身赛(2)——全真模拟

起点到终点的最短路 这里给出3种算法。Floyd。dijkstra和spfa
从提交的代码得速度来看。dijkstra>spfa>Floyd
可是别人都说spfa最快。。。(我就不知道了)
folyd不推荐使用。由于他最慢。,数据大一点的话更不行。

。(只是他最简单)
(1)Floyd

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<stack> #pragma comment(linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define EPS 1e-6 #define INF (1<<28) using namespace std; int cost[205][205]; bool used[205]; int n,m; int d[205]; void floyd() { for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]); } int main() { while(scanf("%d %d",&n,&m)!=EOF) { int i,j; for(i=0;i<n;i++) { cost[i][i]=0; for(j=0;j<n;j++) { if(i!=j) cost[i][j]=INF; } } int a,b,value,start,endl; for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&value); if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。

} scanf("%d %d",&start,&endl); floyd(); if(cost[start][endl]!=INF) printf("%dn",cost[start][endl]); else printf("-1n"); } return 0; }

2:dijkstra

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<stack> #pragma comment(linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define EPS 1e-6 #define INF (1<<28) using namespace std; int cost[205][205]; bool used[205]; int n,m; int d[205]; void dijkstra(int s) { fill(d,d+n,INF); fill(used,used+n,false); d[s]=0; while(true) { int v=-1; for(int u=0;u<n;u++) { if(!used[u]&&(v==-1||d[u]<d[v])) v=u; } if(v==-1) break; used[v]=true; for(int u=0;u<n;u++) { d[u]=min(d[u],d[v]+cost[v][u]); } } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { int i,j; for(i=0;i<n;i++) { cost[i][i]=0; for(j=0;j<n;j++) { if(i!=j) cost[i][j]=INF; } } int a,b,value,start,endl; for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&value); if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。

} scanf("%d %d",&start,&endl); dijkstra(start); if(d[endl]!=INF) printf("%dn",d[endl]); else printf("-1n"); } return 0; }

3:spfa

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<stack> #pragma comment(linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define EPS 1e-6 #define INF (1<<28) using namespace std; int cost[205][205]; bool used[205]; int n,m; int d[205]; void SPFA(int src,int des) { int i; for(i=0;i<n;i++) d[i]=INF; memset(used,false,sizeof(used)); queue<int> myqueue; while(!myqueue.empty()) myqueue.pop();//清空队列 d[src]=0; used[src]=1; myqueue.push(src); int tmp; while(!myqueue.empty()) { tmp=myqueue.front(); myqueue.pop(); used[tmp]=0; for(i=0;i<n;i++) if(d[i]>d[tmp]+cost[tmp][i]) { d[i]=d[tmp]+cost[tmp][i]; if(!used[i]) { used[i]=1; myqueue.push(i); } } } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { int i,j; for(i=0;i<n;i++) { cost[i][i]=0; for(j=0;j<n;j++) { if(i!=j) cost[i][j]=INF; } } int a,b,value,start,endl; for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&value); if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。 } scanf("%d %d",&start,&endl); SPFA(start,endl); if(d[endl]!=INF) printf("%dn",d[endl]); else printf("-1n"); } return 0; }

最后

以上就是拼搏秋天最近收集整理的关于hdu1874 畅通project续 最短路 floyd或dijkstra或spfa的全部内容,更多相关hdu1874内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部