标签:
1 #include <stdio.h> 2 #include <algorithm> 3 int n, q; 4 double safe[1010], sa[1010][1010]; 5 int vis[1010]; 6 double max(double x, double y) 7 { 8 return x > y ? x : y; 9 } 10 void dijkstra(int s, int t) 11 { 12 int u, v; 13 for(u = 1; u <= n; u++) 14 { 15 vis[u] = 0; 16 safe[u] = 0.0; 17 } 18 safe[s] = 1.0; 19 while(true) 20 { 21 v= -1; 22 for(u = 1; u <= n; u++) 23 if(!vis[u] && (safe[u]>safe[v] || v==-1)) 24 v = u; 25 if(v == -1) 26 break; 27 vis[v] = 1; 28 for(u = 1; u <= n; u++) 29 { 30 safe[u] = max(safe[u], safe[v]*sa[v][u]); 31 } 32 } 33 if(safe[t] == 0.0) 34 printf("What a pity!\n"); 35 else 36 printf("%.3f\n", safe[t]); 37 } 38 int main() 39 { 40 double s; 41 while(~scanf("%d", &n)) 42 { 43 for(int i = 1; i <= n; i++) 44 for(int j = 1; j <= n; j++) 45 { 46 scanf("%lf", &s); 47 if(i <= j) 48 sa[i][j] = sa[j][i] = s; 49 } 50 scanf("%d", &q); 51 while(q--) 52 { 53 int a, b; 54 scanf("%d%d", &a, &b); 55 dijkstra(a, b); 56 } 57 } 58 return 0; 59 }
hdoj 1596 find the safest rode
标签:
原文地址:http://www.cnblogs.com/digulove/p/4738523.html