HDU 1596 find the safest road(Dijkstra)
http://acm.hdu.edu.cn/showproblem.php?pid=1596
题意: XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 ^_^
分析:
直接用Dijkstra算法的简化版求即可.不过距离相加,变成了安全系数相乘了.(用Floyd算法超时,如果用vector版本的Dijkstra算法,在添加边的时候也会超时)
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
int n;
double d[maxn];
double map[maxn][maxn];
double dijkstra(int s,int e)
{
bool done[maxn];
memset(done,0,sizeof(done));
for(int i=1;i<=n;i++) d[i]=i==s?1.0:0;
for(int i=1;i<=n;i++)
{
int x; double min_val=0.0;
for(int y=1;y<=n;y++)if(!done[y] && min_val<=d[y]) min_val = d[x=y];
done[x]=true;
for(int y=1;y<=n;y++) d[y] = max(d[y],d[x]*map[x][y]);
}
return d[e];
}
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%lf",&map[i][j]);
}
int Q; scanf("%d",&Q);
while(Q--)
{
int u,v;
scanf("%d%d",&u,&v);
double ans = dijkstra(u,v);
if(ans==0.0) printf("What a pity!\n");
else printf("%.3lf\n",ans);
}
}
return 0;
}
分享到:
相关推荐
NULL 博文链接:https://128kj.iteye.com/blog/1716470
Please write a program to find the smallest positive integer n that (f(n,m)?n)⊕n=k, or determine it is impossible. Input The first line of the input contains an integer T(1≤T≤10), denoting the ...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu 1166线段树代码
HDU最全ac代码
hdu动态规划算法集锦
自己做的HDU ACM已经AC的题目
hdu题目分类