HDU 1596 find the safest road
来源:互联网 发布:男生用什么面膜 知乎 编辑:程序博客网 时间:2024/06/06 07:33
http://acm.hdu.edu.cn/showproblem.php?pid=1596
1.队列优化的SPFA算法
#include <queue>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int Max = 1010;typedef pair <double,int> pii;int n,m,vis[Max];double safe[Max],map[Max][Max];void SPFA(int scr){///SPFA重复入队,用队列减少无用的松弛 for(int i=1; i<=n; i++) safe[i] = 0, vis[i] = 0; safe[scr] = 1,vis[scr] = 1; queue <int> q; q.push(scr); while(!q.empty()){ int tmp = q.front();q.pop(); vis[tmp]=0; for(int i=1; i<=n; i++){ if(safe[i] < safe[tmp] * map[tmp][i]){ safe[i] = safe[tmp] * map[tmp][i]; if(!vis[i]){ q.push(i); vis[i] = 1; } } } }}int main(){// freopen("in.txt", "r", stdin); while(scanf("%d",&n) == 1 && n){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ scanf("%lf",&map[i][j]); } } scanf("%d",&m); int from, to; for(int i=0; i<m; i++){ scanf("%d%d",&from, &to); SPFA(from); if(!safe[to]) puts("What a pity!"); else printf("%.3lf\n",safe[to]); } } return 0;}2.优先队列优化的dijkstra
#include <queue>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int Max = 1010;typedef pair <double,int> pii;int n,m,vis[Max];double safe[Max],map[Max][Max];void dijkstra(int scr){///每一次贪心选择最优 for(int i=1; i<=n; i++) safe[i] = 0, vis[i] = 0; safe[scr] = 1; priority_queue <pii> q; q.push(make_pair(safe[scr], scr)); while(!q.empty()){ pii tmp = q.top();q.pop(); int from = tmp.second;// cout << from << endl; if(vis[from]) continue; vis[from]=1; for(int i=1; i<=n; i++){ if(safe[i] < safe[from] * map[from][i]){ safe[i] = safe[from] * map[from][i]; q.push(make_pair(safe[i], i)); } } }}int main(){// freopen("in.txt", "r", stdin); while(scanf("%d",&n) == 1 && n){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ scanf("%lf",&map[i][j]); } } scanf("%d",&m); int from, to; for(int i=0; i<m; i++){ scanf("%d%d",&from, &to); dijkstra(from); if(!safe[to]) puts("What a pity!"); else printf("%.3lf\n",safe[to]); } } 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
- 以root身份运行wireshark报错解决
- Foundation框架 —— NSAttributedString
- 推聊-开源Android手机聊天系统
- 检查补丁是否是rolling patch
- Android:实现一种浮动选择菜单的效果
- HDU 1596 find the safest road
- 如何定义AIDL进行跨进程通信
- android API号和系统号的对应
- hdu 1850(妮姆博奕)
- 2747542820
- 电商平台应该怎么做运营
- 2014年互联网IT公司产品、技术类人员工资待遇汇总 2015部分补充
- 移动电源的进步从方案开始
- View的setTag()和getTag()方法的使用