POJ:1122 FDNY to the Rescue!
来源:互联网 发布:淘宝申请退货时间 编辑:程序博客网 时间:2024/06/15 14:29
迪杰斯特拉算法求最短路。注意从i到j和从j到i用时是不同的,也就是是有向图。还有,最后要求的是从消防站到火警位置的最短用时,我因为这里没好好看题调了好久才发现。
最后递归打印路径即可。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;struct Elem{ int _time,_org,_dest;};bool cmp(Elem a,Elem b){ return a._time<b._time;}int p[25]= {0};int pre[25]= {0};void Out_put(int a){ if(pre[a]==-1)return; printf("\t%d",pre[a]); Out_put(pre[a]);}int main(){ int N; while(scanf("%d",&N)!=EOF) { int gl[25][25]= {0}; for(int i=1; i<=N; ++i) for(int j=1; j<=N; ++j) scanf("%d",&gl[i][j]); char c; int n=0; memset(p,0,sizeof(p)); while(scanf("%d%c",&p[n++],&c)&&c!='\n'); int dis[25]; bool used[25]= {0}; memset(used,0,sizeof(used)); memset(dis,0x7f,sizeof(dis)); memset(pre,0,sizeof(pre)); int INF=dis[0]; dis[p[0]]=0; pre[p[0]]=-1; while(true) { int v=-1; for(int i=1; i<=N; ++i) if(!used[i]&&(v==-1||(dis[i]<dis[v]))) v=i; if(v==-1) break; used[v]=true; for(int i=1; i<=N; ++i) if(gl[i][v]!=-1&&dis[v]!=INF&&dis[i]>dis[v]+gl[i][v]) { dis[i]=dis[v]+gl[i][v]; pre[i]=v; } } Elem ans[25]; for(int i=1; i<n; ++i) { ans[i-1]._org=p[i]; ans[i-1]._dest=p[0]; ans[i-1]._time=dis[p[i]]; } sort(ans,ans+n,cmp); puts("OrgDestTimePath"); for(int i=0; i<n-1; ++i) { printf("%d\t%d\t%d\t%d",ans[i]._org,ans[i]._dest,ans[i]._time,ans[i]._org); Out_put(ans[i]._org); printf("\n"); } } return 0;}
- Poj 1122 FDNY to the Rescue
- POJ:1122 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!
- poj 1122||zoj 1053 FDNY to the Rescue!(最短路)
- poj 1122 FDNY to the Rescue! 最短路
- POJ--1122--FDNY to the Rescue!【最短路】
- poj 1122 FDNY to the Rescue! (dijkstra)
- POJ 1122 FDNY to the Rescue! 反向dijkstra
- zoj 1053 && poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!(最短路径)
- POJ 1122 FDNY to the Rescue! 已被翻译
- poj1122 - FDNY to the Rescue!
- poj 1122 FDNY to the Rescue! (最短路+打印路径)
- ZPJ - 1053 FDNY to the Rescue!(最短路 Dijkstra),POJ 1122
- poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
- 菜鸟学Android之环境搭建及HelloWorld
- Linux环境下用户空间与内核空间数据的交换方式
- KMP算法(改进版)
- Android自定义类似ProgressDialog效果的Dialog
- adaboost
- POJ:1122 FDNY to the Rescue!
- Linux中重定向及管道
- qt QSettings 用法总结
- 青铜器软件创始人谈:青铜器RDM的创业打造历程?
- 威尔逊定理
- java-一个简单的访问DB的main方法使用
- gluLookAt() gluPerspective()
- PC机也能做服务器
- 经典聚类算法及在互联网的应用