hdu1596 find the safest road(最短路)
来源:互联网 发布:c语言大小写转换程序 编辑:程序博客网 时间:2024/06/16 07:21
hdu 1596 find the safest road
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
3
1 0.5 0.5
0.5 1 0.4
0.5 0.4 1
3
1 2
2 3
1 3
Sample Output
0.500
0.400
0.500
题目大意:中文题目。
解题思路:变形的最短路问题。用dijkstra就能搞。
#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace std;const int N = 1005;const int M = 1000005;typedef long long ll;int n, s, t;double Gra[N][N];double d[N];int vis[N];void input() { double num; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%lf", &num); Gra[i][j] = num; } } }void dij() { memset(vis, 0, sizeof(vis)); for (int i = 0; i <= n; i++) { d[i] = 0; } d[s] = 1; for (int i = 1; i <= n; i++) { int x; double m = 0; for (int y = 1; y <= n; y++) { if (!vis[y] && d[y] > m) m = d[x = y]; } vis[x] = 1; for (int y = 1; y <= n; y++) { if (x == y || Gra[x][y] == 0) continue; d[y] = max(d[y], d[x] * Gra[x][y]); } }}int main() { while (scanf("%d", &n) != EOF) { int T; input(); scanf("%d", &T); while (T--) { scanf("%d %d", &s, &t); dij(); if (d[t] < 1e-9) { printf("What a pity!\n"); } else printf("%.3lf\n", d[t]); } } return 0;}
0 0
- find the safest road hdu1596 最短路
- HDU1596 find the safest road(最短路)
- hdu1596 find the safest road(最短路)
- hdu1596 find the safest road(最短路)
- hdu1596 find the safest road 最短路dijkstra
- hdu1596 find the safest road 最短路也能求最大值
- HDU1596 find the safest road 最短路Floyd算法
- hdu1596 find the safest road【最短路dijkstra&&SPFA】
- hdu1596 find the safest road(dijkstra求最短路)
- HDU1596 find the safest road(最短路变形,Dijkstra)
- hdu1596 find the safest road(最短路径floyd)
- HDU1596 find the safest road(floyd)
- hdu1596 find the safest road
- hdu1596 find the safest road
- HDU1596---find the safest road
- hdu1596 find the safest road
- [hdu1596] find the safest road
- HDU1596 find the safest road
- 从ftp服务器下载文件
- 基于Spark的用户分析系统
- 父类和子类的转化
- hadoop-2.6.0集群开发环境配置
- 达内学习日志 Day01(上):职业发展规划之态度
- hdu1596 find the safest road(最短路)
- 多线程四(同步函数)
- LeetCode 14: Longest Common Prefix
- Java基础-----集合
- Oracle之DataGurd(DG)环境搭建
- 客户端网页编程--第五章
- ios8新特性widget开发
- 动态生成页面(二)
- DB---环境搭建