USACO 2.4 Bessie Come Home
mark:就是个最短路。。。错了2次,一次是题目没读清楚,一次是自己犯2。。。
代码:
1 # include <stdio.h> 2 3 4 int graph[210][210] ; 5 int INF = 0x0f0f0f0f ; 6 7 8 int min(int a, int b){return a<b?a:b;} 9 10 11 int main () 12 { 13 char ch1, ch2 ; 14 int num, p, pos, i, j, k ; 15 16 freopen ("comehome.in", "r", stdin) ; 17 freopen ("comehome.out", "w", stdout) ; 18 19 while (~scanf ("%d%*c", &p)) 20 { 21 for (i = 0 ; i < 200 ; i++) 22 { 23 for (j = 0 ; j < 200 ; j++) 24 if (i != j) graph[i][j] = INF ; 25 else graph[i][j] = 0 ; 26 } 27 while (p--) 28 scanf ("%c %c %d%*c", &ch1, &ch2, &num), 29 graph[ch1][ch2] = graph[ch2][ch1] = min(graph[ch1][ch2], num) ; 30 31 for (k = 0 ; k <= 'z' ; k++) 32 for (i = 0 ; i <= 'z' ; i++) 33 for (j = 0 ; j <= 'z' ; j++) 34 if (graph[i][k]+graph[k][j] < graph[i][j]) 35 graph[i][j] = graph[i][k]+graph[k][j] ; 36 pos = -1 ; 37 for (i = 'A' ; i < 'Z' ; i++) 38 if (pos == -1 || graph[i]['Z'] < graph[pos]['Z']) pos = i ; 39 printf ("%c %d\n", pos, graph[pos]['Z']) ; 40 } 41 return 0 ; 42 }