HDU 1596 find the safest road
来源:互联网 发布:数控螺纹编程书 编辑:程序博客网 时间:2024/06/03 16:32
题目链接
题目意思
给你n个城市,接着是n*n的矩阵,记录着城市到城市之间道路的安全系数。安全系数为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 。现在给你q个询问,接着给出你两个城市,让你输出这两个城市之间最安全的道路的安全系数。
解题思路
这相当于一道Dijkstra的模板题,只要稍微灵活运用就行。要求最安全的道路就是要算安全系数最大的道路。
代码部分
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n;double maps[1010][1010];double dis[1010];///源点到i的最大安全系数bool vis[1010];///标记数组double dijkstra(int u,int v)///u代表源点,v代表终点{ for(int i=1; i<=n; i++) { dis[i]=maps[u][i]; vis[i]=false; } int pos=u; vis[u]=true;///标记源点 for(int i=1; i<=n; i++) { double maxn=0; for(int j=1; j<=n; j++) { if(!vis[j]&&dis[j]>maxn) { maxn=dis[j]; pos=j; } } vis[pos]=true; for(int j=1; j<=n; j++)///更新dis数组 { if(!vis[j]&&dis[j]<maps[pos][j]*dis[pos]) dis[j]=maps[pos][j]*dis[pos]; } if(pos==v) return dis[v]; } return 0;}int main(){ int a,b,q; double temp; while(~scanf("%d",&n)) { memset(maps,0,sizeof(maps)); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) scanf("%lf",&maps[i][j]); scanf("%d",&q); for(int i=1; i<=q; i++) { scanf("%d%d",&a,&b); temp=dijkstra(a,b); if(temp) printf("%.3lf\n",temp); else printf("What a pity!\n"); } } return 0;}
阅读全文
0 0
- hdu 1596 find the safest road(priority_queue )
- HDU 1596 find 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
- HDU 1596 find the safest road
- HDU 1596 find the safest road
- hdu 1596find the safest road--dijkstra
- hdu 1596 find 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
- hdu 1596 find the safest road
- hdu-1596 find the safest road
- HDU 1596 find the safest road(Dijkstra)
- HDU 1596 find the safest road
- QQ第三方登录获取头像
- 2017第十五届欧亚中国郑州国际酒店用品博览会会刊(参展商名录)
- 机器视觉的相机标定
- ldconfig与 /etc/ld.so.conf
- 海康威视笔试准备题
- HDU 1596 find the safest road
- io流 输入流 输出流 字符流
- Hibernate 二级缓存
- mysql 字段as详解及实例代码
- promise在es6与es7中的不同
- spark 资源参数调优
- openpyxl把空白行中的内容填写指定的内容
- Linux配置SSH免密码登录
- java从 .pfx或.p12文件中获取公钥和私钥