HDU-1596-find the safest road(最短路)
来源:互联网 发布:数学在线解题软件 编辑:程序博客网 时间:2024/05/01 22:58
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596
find the safest road
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6822 Accepted Submission(s): 2430
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所在的城市和要去的城市
第一行: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
和prim算法比,dijkstra就改变了low数组的意义,prim保存的是该点到与之相连的所有的距离,并用mmin保存最小的距离;而dijkstra保存的是单源最短路到所有点的全局最短路径,所以更新low数组时要比较map[][]+mmin的大小,从而更新low 数组
#include <stdio.h>#include <stdlib.h>#include <string.h>int cityNum;double map[1010][1010];double low[1010];int vis[1010];void dijkstra(int x){ vis[x]=1; int i,j; for(i=1;i<=cityNum;i++) low[i]=map[x][i]; for(i=1;i<cityNum;i++) { double mmax = 0; int temp; for(j=1;j<=cityNum;j++) { if(!vis[j] && mmax < low[j]) { temp = j; mmax = low[j]; } } vis[temp]=1; for(j=1;j<=cityNum;j++) { if(!vis[j] && low[j] < (map[temp][j]*mmax)) low[j] = map[temp][j]*mmax; } }}int main(){ while(scanf("%d",&cityNum)!=EOF) { int i,j; for(i=1;i<=cityNum;i++) { for(j=1;j<=cityNum;j++) { scanf("%lf",&map[i][j]); } } int qu; scanf("%d",&qu); int x,y; for(i=0;i<qu;i++) { memset(vis,0,sizeof(vis)); scanf("%d %d",&x,&y); dijkstra(x);//计算从x出发到Y点的最短距离,用low[y]保存 if(low[y]!=0) printf("%.3lf\n",low[y]); else printf("What a pity!\n"); } } return 0;}
0 0
- hdu 1596 find the safest road (最短路)
- HDU 1596 find the safest road 最短路求最大值
- HDU 1596find the safest road(最短路变形)
- hdu 1596 find the safest road(乘积最短路)
- HDU-1596-find the safest road(最短路)
- HDU 1596 find the safest road (最短路)
- hdu 1596 find the safest road 最短路 FLoyd算法
- HDU-1596 find the safest road(最短路[Dijkstra])
- HDU 1596 find the safest road(最短路变形)
- HDU Problem 1596 find the safest road【最短路dijkstra】
- HDU 1596 find the safest road (最短路)
- 【HDU】-find the safest road(最短路)(dijkstra)
- 两道最短路裸题(HDU 2544 最短路 / HDU 1596 find the safest road)
- find the safest road(floyd最短路)
- 最短路-find the safest road
- find the safest road hdu1596 最短路
- HDU1596 find the safest road(最短路)
- 【最短路+dijkstra+floyd+spfa】1596 find the safest road
- 打包文件
- sun.misc.BASE64Encoder找不到jar包的解决方法
- VM中安装vmtools以实现文件共享
- Combinations
- c# 异常
- HDU-1596-find the safest road(最短路)
- How to Install Oracle Java JDK 6/7/8 on Ubuntu 13.04 / 12.10 / 12.04
- Dialog不能全屏查看源码的错误总结。
- 我裂解的机器学习(ML)
- 第一个ios应用(4)
- java中有那几个类负责对字符进行操作?
- Post请求登录笔记(服务器和客户端示例源码)
- Objective-C语法之NSSet和NSMutableSet
- 数据库隔离级别