Dijkstra。
1 /* 1601 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define INF 999999 7 8 char buf[27]; 9 int map[27][27]; 10 bool visit[27], valid[27]; 11 double val[27]; 12 int path[27]; 13 int n; 14 15 void dijkstra() { 16 int i, j, k; 17 int min, v; 18 19 memset(visit, false, sizeof(visit)); 20 visit[26] = true; 21 for (i=0; i<27; ++i) 22 path[i] = map[i][26]; 23 24 for (i=0; i<26; ++i) { 25 min = INF; 26 for (j=0; j<26; ++j) { 27 if (!visit[j] && path[j]<min) { 28 min = path[j]; 29 v = j; 30 } 31 } 32 if (min == INF) 33 break; 34 visit[v] = true; 35 for (j=0; j<26; ++j) { 36 if (!visit[j] && path[j]>min+map[v][j]) { 37 path[j] = min + map[v][j]; 38 } 39 } 40 } 41 } 42 43 int main() { 44 int i, j, k, r; 45 double v, max; 46 char cmd[3], line[30]; 47 48 #ifndef ONLINE_JUDGE 49 freopen("data.in", "r", stdin); 50 freopen("data.out", "w", stdout); 51 #endif 52 53 while (scanf("%d",&n)!=EOF) { 54 for (i=0; i<27; ++i) { 55 for (j=0; j<27; ++j) { 56 map[i][j] = INF; 57 } 58 map[i][i] = 0; 59 } 60 for (i=0; i<n; ++i) { 61 scanf("%s %lf %s", cmd, &v, line); 62 j = cmd[0] - ‘A‘; 63 val[j] = v; 64 for (r=0; line[r]; ++r) { 65 if (line[r] == ‘*‘) 66 k = 26; 67 else 68 k = line[r] - ‘A‘; 69 map[j][k] = map[k][j] = 1; 70 } 71 } 72 dijkstra(); 73 max = -1; 74 v = 0; 75 for (i=0; i<26; ++i) { 76 if (path[i] >= INF) 77 continue; 78 if (path[i] > 1) { 79 j = path[i]-1; 80 while (j--) { 81 val[i] *= 0.95; 82 } 83 } 84 if (val[i] > max) { 85 max = val[i]; 86 v = i; 87 } 88 } 89 printf("Import from %c\n", (char)v+‘A‘); 90 } 91 92 return 0; 93 }
原文:http://www.cnblogs.com/bombe1013/p/4296142.html