图论trainning-part-1 A. 最短路

A. 最短路

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format:  %I64d      Java class name: Main
 
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

 

Input

输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
 

Output

对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
 

Sample Input

2 1

1 2 3

3 3

1 2 5

2 3 5

3 1 2

0 0

Sample Output

3

2


解题:最短路。。。


图论trainning-part-1 A. 最短路
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #define LL long long

13 #define INF 0x3f3f3f

14 using namespace std;

15 int mp[110][110],d[110],n,m;

16 bool vis[110];

17 void dij(){

18     int i,j,theMin,index;

19     for(i = 0; i <= n; i++)

20         d[i] = INF>>1;

21     d[1] = 0;

22     memset(vis,false,sizeof(vis));

23     for(i = 0; i < n; i++){

24         theMin = INF;

25         for(j = 1; j <= n; j++){

26             if(!vis[j] && theMin > d[j]) theMin = d[index = j];

27         }

28         vis[index] = true;

29         if(index == n) break;

30         for(j = 1; j <= n; j++){

31             if(!vis[j] && d[j] > d[index]+mp[index][j])

32                 d[j] = d[index] + mp[index][j];

33         }

34     }

35 }

36 int main(){

37     int i,j,u,v,w;

38     while(scanf("%d%d",&n,&m),n||m){

39         for(i = 0; i <= n; i++)

40         for(j = 0; j <= n; j++)

41             mp[i][j] = INF;

42         for(i = 0; i < m; i++){

43             scanf("%d%d%d",&u,&v,&w);

44             mp[u][v] = mp[v][u] = w;

45         }

46         dij();

47         printf("%d\n",d[n]);

48     }

49     return 0;

50 }
View Code

 

优先队列版Dijkstra

 

图论trainning-part-1 A. 最短路
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cstdlib>

 5 #include <vector>

 6 #include <queue>

 7 #include <climits>

 8 #include <algorithm>

 9 #include <cmath>

10 #define LL long long

11 #define INF 0x3f3f3f

12 using namespace std;

13 #define pii pair<int,int>

14 const int maxn = 102;

15 struct arc {

16     int to,w;

17 };

18 int n,m;

19 vector<arc>g[maxn];

20 bool done[maxn];

21 int d[maxn];

22 priority_queue<pii,vector< pii >,greater< pii > >q;

23 void dj() {

24     int i,j;

25     for(i = 1; i <= n; i++) {

26         done[i] = false;

27         d[i] = INF;

28     }

29     d[1] = 0;

30     while(!q.empty()) q.pop();

31     q.push(make_pair(d[0],1));

32     while(!q.empty()) {

33         pii u =q.top();

34         q.pop();

35         int x = u.second;

36         if(done[x]) continue;

37         done[x] = true;

38         for(i = 0; i < g[x].size(); i++) {

39             j = g[x][i].to;

40             if(d[j] > d[x] + g[x][i].w) {

41                 d[j] = d[x]+g[x][i].w;

42                 q.push(make_pair(d[j],j));

43             }

44         }

45     }

46 

47 }

48 int main() {

49     int i,j,u,v,w;

50     while(scanf("%d%d",&n,&m),n||m) {

51         for(i = 1; i <= n; i++)

52             g[i].clear();

53         for(i = 0; i < m; i++) {

54             scanf("%d%d%d",&u,&v,&w);

55             g[u].push_back((arc) {v,w});

56             g[v].push_back((arc) {u,w});

57         }

58         dj();

59         printf("%d\n",d[n]);

60     }

61     return 0;

62 }
View Code

 

Bellman—Ford算法:

 

图论trainning-part-1 A. 最短路
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cstdlib>

 5 #include <vector>

 6 #include <queue>

 7 #include <climits>

 8 #include <algorithm>

 9 #include <cmath>

10 #define LL long long

11 #define INF 0x3f3f3f

12 using namespace std;

13 #define pii pair<int,int>

14 const int maxn = 10010;

15 struct arc {

16     int u,v,w;

17 } g[maxn];

18 int n,m,cnt;

19 int d[maxn];

20 void bf() {

21     int i,j;

22     for(i = 1; i <= n; i++)

23         d[i] = INF;

24     d[1] = 0;

25     bool flag;

26     for(i = 1; i < n; i++) {

27         flag = true;

28         for(j = 0; j < cnt; j++)

29             if(d[g[j].v] > d[g[j].u]+g[j].w){

30                  d[g[j].v] = d[g[j].u]+g[j].w;

31                  flag = false;

32             }

33         if(flag) break;

34     }

35 }

36 int main() {

37     int i,u,v,w;

38     while(scanf("%d%d",&n,&m),n||m) {

39         for(cnt = i = 0; i < m; i++) {

40             scanf("%d%d%d",&u,&v,&w);

41             g[cnt++] = (arc) {u,v,w};

42             g[cnt++] = (arc) {v,u,w};

43         }

44         bf();

45         printf("%d\n",d[n]);

46     }

47     return 0;

48 }
View Code

 

spfa算法版:

图论trainning-part-1 A. 最短路
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cstdlib>

 5 #include <vector>

 6 #include <queue>

 7 #include <climits>

 8 #include <algorithm>

 9 #include <cmath>

10 #define LL long long

11 #define INF 0x3f3f3f

12 using namespace std;

13 #define pii pair<int,int>

14 const int maxn = 1001;

15 struct arc {

16     int to,w;

17 };

18 vector<arc>g[maxn];

19 queue<int>q;

20 int n,m,d[maxn];

21 bool done[maxn];

22 void spfa() {

23     int i,j;

24     for(i = 1; i <= n; i++){

25         d[i] = INF;

26         done[i] = false;

27     }

28     d[1] = 0;

29     while(!q.empty()) q.pop();

30     done[1] = true;

31     q.push(1);

32     while(!q.empty()){

33         int temp = q.front();

34         q.pop();

35         done[temp] = false;

36         for(i = 0; i < g[temp].size(); i++){

37             j = g[temp][i].to;

38             if(d[j] > d[temp]+g[temp][i].w){

39                 d[j] = d[temp]+g[temp][i].w;

40                 if(!done[j]){

41                     q.push(j);

42                     done[j] = true;

43                 }

44             }

45         }

46     }

47 

48 }

49 int main() {

50     int i,u,v,w;

51     while(scanf("%d%d",&n,&m),n||m) {

52         for(i = 1; i <= n; i++)

53             g[i].clear();

54         for(i = 0; i < m; i++) {

55             scanf("%d%d%d",&u,&v,&w);

56             g[u].push_back((arc){v,w});

57             g[v].push_back((arc){u,w});

58         }

59         spfa();

60         printf("%d\n",d[n]);

61     }

62     return 0;

63 }
View Code

 

 

你可能感兴趣的:(part)