【杭电1596】find the safest road

来源:互联网 发布:php array splice 编辑:程序博客网 时间:2024/05/20 05:22

                                   find the safest road

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11708    Accepted Submission(s): 4143


Problem Description
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
 

Author
ailyanlu
 

Source
HDU 2007-Spring Programming Contest - Warm Up (1)
 

Recommend
8600   |   We have carefully selected several similar problems for you:  1217 1598 1142 1690 2544 
 


最短路的变种,还是挺不错的这种题,提升思维能力。
<pre name="code" class="cpp">#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;const int N = 1010;const int inf = 0x3f3f3f3f;double map[N][N],cast[N];int n,vis[N];void dijkstra(int a) {memset(vis,0,sizeof(vis));memset(cast,0,sizeof(cast));int flag=0;cast[a]=1;for(int l=1; l<=n; l++) {double maxn=0;int k=0;for(int i=1; i<=n; i++) {if(maxn<cast[i]&&!vis[i]) {maxn=cast[i];k=i;}}vis[k]=1;for(int j=1; j<=n; j++) {if(!vis[j]&&cast[j]<cast[k]*map[k][j]) {cast[j]=cast[k]*map[k][j];}}}}int main() {while(scanf("%d",&n)!=EOF) {for(int l=1; l<=n; l++) {for(int j=1; j<=n; j++) {scanf("%lf",&map[l][j]);}}int m;scanf("%d",&m);while(m--) {int a,b;scanf("%d%d",&a,&b);dijkstra(a);if(cast[b]==0)printf("What a pity!\n");elseprintf("%.3lf\n",cast[b]);}}return 0;}
点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1596


0 0
原创粉丝点击