HDU 1596 find the safest road (最短路)
2017-11-08 11:00
369 查看
【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=1596
http://acm.hdu.edu.cn/showproblem.php?pid=1596
题目意思
题目大意:有n个城市,没两个城市之间都有个安全系数,0代表没路。现在有q次询问,每次询问给一个起点一个终点,问起点到终点最大安全系数的路是多少解题思路
一题最短路的题,只是把选择最短的路改变成选择最大安全系数的路,更新条件改变下代码部分
#include <bits/stdc++.h> using namespace std; #define LL long long const int maxn=1005; const int INT=0x3f3f3f; int n,m; float a[maxn][maxn]; int vis[maxn]; float dis[maxn]; float Dis(int s,int t) { memset(vis,0,sizeof(vis)); vis[s]=1; int j; float mi=0; for (int i=0; i<n; i++) { dis[i]=a[s][i]; if (a[s][i]>mi&&!vis[i]) ///寻找最安全的路 { mi=a[s][i]; j=i; } } if (mi!=0) { dis[j]=mi; } else return -1; for (int i=0; i<n-1; i++) { s=j; mi=0; vis[j]=1; if (j==t) { return dis[t]; } for (int k=0; k<n; k++) ///更新每个点最安全系数 { if (!vis[k]) { if (a[s][k]*dis[s]>dis[k]) dis[k]=a[s][k]*dis[s]; } } for (int k=0; k<n; k++) ///寻找最安全的路 { if (dis[k]>mi&&!vis[k]) { mi=dis[k]; j=k; } } } return -1; } int main() { while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); memset(dis,0,sizeof(dis)); for (int i=0; i<n; i++) for (int j=0; j<n; j++) scanf("%f",&a[i][j]); int q; scanf("%d",&q); for (int i=0; i<q; i++) ///q次询问跑q次最短路 { int s,t; scanf("%d %d",&s,&t); float d=Dis(s-1,t-1); if (d!=-1) printf("%.3f\n",d); else cout<<"What a pity!"<<endl; } } return 0; }
相关文章推荐
- HDU 1596 find the safest road 最短路
- HDU 1596 find the safest road (最短路)
- 两道最短路裸题(HDU 2544 最短路 / HDU 1596 find the safest road)
- hdu 1596 find the safest road(最短路求最大值的题目,有两种稍微不同的处理方式)
- HDU - 1596 find the safest road (最短路水题)
- hdu1596 find the safest road 最短路dijkstra
- HDU 1596 find the safest road (最短路)
- HDU-1596 find the safest road(最短路[Dijkstra])
- HDU-1596-find the safest road(最短路)
- hdu 1596 find the safest road(最短路)
- HDU 1596(find the safest road) 最短路(SPFA&FLOYD&Dijkstra)
- hdu 1596 find the safest road(最短路,模版题)
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- hdu 1596 find the safest road(乘积最短路)
- hdu 1596 find the safest road (最短路)
- Hdu 1596 find the safest road 变相最短路径 Dijstra算法
- HDU - 1596 find the safest road (dijkstra)
- Hdu 1596 find the safest road
- hdu 1596 find the safest road (floyd)
- hdu 1596 find the safest road