ZOJ 3166题解 最短路径之Floyd算法

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define Maxint 9999999
5 #define Max 101
6
7 int main()
8 {
9 int M, N, hotel[101], a, C, b, t;
10 int i, j, k, r, sum, dist[101][101];
11
12 while (scanf("%d%d",&N, &C) != EOF)
13 {
14 for (i=1; i<=N; i++)
15 for (j=1; j<=N; j++)
16 {
17 dist[i][j] = Maxint;
18 }
19 sum = Maxint;
20 for (i=1; i<=C; i++)
21 {
22 scanf("%d",&hotel[i]);
23 }
24 scanf("%d", &M);
25 for(i=0; i<M; i++)
26 {
27 scanf("%d%d%d", &a, &b, &t);
28 dist[a][b] = t;
29 }
30 for (k=1; k<=N; k++)
31 {
32 for (i=1; i<=N; i++)
33 for (j=1; j<=N; j++)
34 {
35 if(dist[i][j] > dist[i][k]+dist[k][j])
36 dist[i][j] = dist[i][k] + dist[k][j];
37 }
38 }
39 r = 0;
40 for (i=1; i<=C; i++)
41 {
42 if (dist[hotel[i]][hotel[i]] < sum)
43 {
44 sum = dist[hotel[i]][hotel[i]];
45 r = hotel[i];
46 }
47 }
48 if( r )
49 printf("%d\n",r);
50 else
51 printf("I will nerver go to that city!\n");
52 }
53 return 0;
54 }
55

你可能感兴趣的:(floyd)