【SPFA】 hdu1596 find the safest road
来源:互联网 发布:存储过程怎么执行sql 编辑:程序博客网 时间:2024/04/30 18:57
find the safest road
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1596
题意:一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边,找一条最安全的路。
题解:用spfa求最短路。
代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define inf 0x7ffffffdouble cost[1005][1005];double dist[1005];bool visit[1005];int n,m;void spfa(int now){ memset(visit,false,sizeof(visit)); memset(dist,0,sizeof(dist)); dist[now]=1; queue<int> q; q.push(now); for(; !q.empty();) { now=q.front(); q.pop(); visit[now]=false; for(int i=1; i<=n; ++i) { if(i==now) continue; if(dist[i]<dist[now]*cost[now][i]) { dist[i]=dist[now]*cost[now][i]; if(!visit[i]) { q.push(i); visit[i]=true; } } } } return;}int main(){ int a,b; for(; ~scanf("%d",&n);) { for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) scanf("%lf",&cost[i][j]); scanf("%d",&m); for(; m--;) { scanf("%d%d",&a,&b); spfa(a); if(dist[b]==0) puts("What a pity!"); else printf("%.3f\n",dist[b]); } } return 0;}
来源:http://blog.csdn.net/ACM_Ted
- 【SPFA】 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
- 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【最短路dijkstra&&SPFA】
- find the safest road hdu1596 最短路
- HDU1596:find the safest road(Dijkstra)
- hdu1596——find the safest road
- hdu1596 find the safest road--多源最短路径
- HDU1596 find the safest road(floyd)
- HDU1596 find the safest road(最短路)
- hdu1596 find the safest road 最短路dijkstra
- 新来
- 二分查找变形
- 【Linux】Redhat5.4基本使用
- Java批处理写数据库的实例代码
- Android(7) - 控件(View)之ZoomControls, Include, VideoView, WebView, RatingBar, Tab, Spinner, Chronomete
- 【SPFA】 hdu1596 find the safest road
- 永不停止的技术学习
- uva 10341 - Solve It
- hdu 2845 Beans
- Android EditText 详解Android2.2 API 中文文档系列(2) —— EditText
- paip.提升安全性-----时间判断
- Android2.2 API 中文文档系列(5) —— View详解
- Graph
- Android API 中文(13) —— ToggleButton