首页 > 代码库 > HDU 1596 find the safest road

HDU 1596 find the safest road

find the safest road

Time Limit: 5000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1596
64-bit integer IO format: %I64d      Java class name: Main
 
XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 ^_^
 

Input

输入包括多个测试实例,每个实例包括:
第一行:n。n表示城市的个数n<=1000;
接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
 

Output

如果86无法达到他的目的地,输出"What a pity!",
其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
 

Sample Input

31 0.5 0.50.5 1 0.40.5 0.4 131 22 31 3

Sample Output

0.5000.4000.500

Source

HDU 2007-Spring Programming Contest - Warm Up (1)
 
解题:貌似用单源最短路路效率更高。Floyd险过。。。
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1010;18 double mp[maxn][maxn];19 int n,m;20 int main() {21     int i,j,k;22     while(~scanf("%d",&n)){23         for(i = 1; i <= n; i++){24             for(j = 1; j <= n; j++)25                 scanf("%lf",mp[i]+j);26         }27         for(k = 1; k <= n; k++){28             for(i = 1; i <= n; i++){29                 for(j = 1; j <= n; j++){30                     double ret = mp[i][k]*mp[k][j];31                     if(ret > mp[i][j]) mp[i][j] = ret;32                 }33             }34         }35         scanf("%d",&m);36         for(k = 0; k < m; k++){37             scanf("%d %d",&i,&j);38             if(mp[i][j] == 0) puts("What a pity!");39             else printf("%.3f\n",mp[i][j]);40         }41     }42     return 0;43 }
View Code

 

HDU 1596 find the safest road