hdoj 1596 find the safest road【最短路3种方法】
来源:互联网 发布:客家话输入软件 编辑:程序博客网 时间:2024/05/17 09:22
find the safest road
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10306 Accepted Submission(s): 3637
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
dijkstra:
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Sch(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define mem(a, b) memset(a, (b), sizeof(a))#define INF 0x3f3f3f3f #include<algorithm>using namespace std;const int mx = 1010;int n;double map[mx][mx];double dis[mx];bool vis[mx];void init(){for(int i = 1; i <= n; i++ ){for(int j = 1; j <= n; j++ )map[i][j] = 0;}}void dijkstra(int s, int t){mem(vis, 0);int i, j, k;for(i = 1; i <= n; i++) dis[i] = map[s][i];vis[s] = 1;for(i = 1; i < n; i++){double minn = 0; k = s;for(j = 1; j <= n; j++){if(!vis[j] && minn < dis[j]){minn = dis[j];k = j;}}vis[k] = 1;for(j = 1; j <= n; j++){if(!vis[j]) dis[j] = max(dis[j], dis[k]*map[k][j]);//是乘不是加。。。 }}if(dis[t] != 0)printf("%.3lf\n", dis[t]);elseputs("What a pity!");} int main(){while(Si(n)==1){init();int i,j,k;for(i = 1; i <= n; i++){for(j = 1; j <= n; j++){scanf("%lf", &map[i][j]);}}int m; Si(m);Wi(m){int a, b;scanf("%d%d", &a, &b);if(a == b)printf("1.000\n");elsedijkstra(a, b);}}return 0;}
floyd: 不断的超时。。。。。最后 3759MS AC了~
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Sch(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define mem(a, b) memset(a, (b), sizeof(a))#define INF 0x3f3f3f3f #include<algorithm>using namespace std;const int mx = 1010;int n;double map[mx][mx];void floyd(){int i, j ,k;for(k = 1; k <= n; k++){for(i = 1; i <= n; i++){for(j = 1; j <= n; j++)if(map[i][j] < map[i][k]*map[k][j])map[i][j] = map[i][k]*map[k][j];}}} int main(){while(Si(n)==1){mem(map, 0);int i,j,k;double c;for(i = 1; i <= n; i++){for(j = 1; j <= n; j++){scanf("%lf", &c);map[i][j] = c;}}floyd();int m; Si(m);Wi(m){int s, t;scanf("%d%d", &s, &t);if(s == t)printf("1.000\n");else{if(map[s][t] != 0)printf("%.3lf\n", map[s][t]);elseputs("What a pity!");}}}return 0;}
SPFA:
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#define mem(a, b) memset(a, (b), sizeof(a))#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Pi(a) printf("%d\n", (a))#define INF 0x3f3f3f#include<algorithm>using namespace std;const int mx = 1010;const int mr = 1000*1000;double dis[mx];bool vis[mx];int head[mr], edgenum, n;struct node{int from, to;double val;int next;}; node edge[mr];void init(){edgenum = 0;mem(head, -1);}void add(int a, int b, double c){node E = { a, b, c, head[a] };edge[edgenum] = E;head[a] = edgenum++;}void spfa(int s, int t){queue<int> q;mem(vis, 0); mem(dis, 0);vis[s] = 1;dis[s] = 1;q.push(s);while(!q.empty()){int u = q.front(); q.pop(); vis[u] = 0;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(dis[v] < dis[u]*edge[i].val){dis[v] = dis[u]*edge[i].val;if(!vis[v]){vis[v] = 1;q.push(v);}}}}if(dis[t] != 0) printf("%.3lf\n", dis[t]);elseputs("What a pity!");}void getmap(){double c;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){scanf("%lf", &c);add(i, j, c);//add(j, i, c);//单向道路!!! }}}int main(){while(Si(n)==1){init();getmap();int m; Si(m);Wi(m){int a, b;scanf("%d%d", &a, &b);spfa(a, b);}}return 0;}
1 0
- hdoj 1596 find the safest road【最短路3种方法】
- HDOJ--1596--find the safest road【最短路】
- HDOJ find the safest road 1596【最短路变形】
- HDOJ 1596 find the safest road(floyd最短路)
- HDOJ 1596 find the safest road(最短路)
- HDOJ 题目1596 find the safest road(最短路Floyd)
- HDOJ 1596 find the safest road ((最短路变形) Dijkstra && SPFA)
- HDOJ 1596 find the safest road(最短路--dijkstra变形)
- HDOJ 1596 find the safest road(类最短路问题,dijkstra算法)
- 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
- hdu 1596 find the safest road (最短路)
- HDU 1596 find the safest road 最短路求最大值
- HDU 1596find the safest road(最短路变形)
- hdu 1596 find the safest road(乘积最短路)
- iOS之UIView加边框及边框颜色
- 机制与策略
- 【MinaFile】【五】1.0版本
- 细心很重要
- JSON解析
- hdoj 1596 find the safest road【最短路3种方法】
- PPAPI与Browser间使用AsyncIPC通信
- 人工智能、应用、程序
- 字符串替换-K&R C 题3-2
- iOS9 升级后UIAlertController的简单使用
- hdu 1325 Is It A Tree? 判断是否是树 巨坑
- poj3233 Matrix Power Series (矩阵快速幂+二分)
- 关于accept 返回的socket的阻塞属性
- setInterval和setTimeout