http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1956&cid=1151
floyd: 用 g++ 提交
View Code
1 #include <stdio.h> 2 #include <string.h> 3 const int inf = 1<<28; 4 const int maxn = 1001; 5 int map[maxn][maxn]; 6 int n ; 7 void floyd() 8 { 9 for(int k = 1; k <= n; k++) 10 { 11 for(int i = 1; i <= n; i++) 12 { 13 for(int j = 1; j <= n; j++) 14 { 15 if(map[i][k]+map[k][j] < map[i][j]) 16 { 17 map[i][j] = map[i][k]+map[k][j] ; 18 } 19 } 20 } 21 } 22 } 23 24 void init() 25 { 26 for(int i = 1; i <= n; i++) 27 { 28 for(int j = 1; j <= n; j++) 29 { 30 map[i][j] = inf; 31 } 32 map[i][i] = 0; 33 } 34 } 35 36 int main() 37 { 38 while(~scanf("%d", &n)) 39 { 40 init(); 41 int a; 42 int s, t ; 43 for(int i = 1; i <= n-1; i++) 44 { 45 scanf("%d", &a); 46 map[i][i+1] = a; 47 map[i+1][i] = a; 48 } 49 scanf("%d",&a); 50 map[n][1] = a; 51 map[1][n] = a; 52 floyd(); 53 scanf("%d %d", &s, &t); 54 if(map[s][t]>map[t][s]) 55 printf("%d\n", map[t][s]); 56 else printf("%d\n",map[s][t]); 57 58 } 59 return 0; 60 } 61