POJ--1122--FDNY to the Rescue!【最短路】
来源:互联网 发布:大数据薪资待遇 编辑:程序博客网 时间:2024/06/11 14:22
题意:给你一个邻接矩阵信息,某点发生火灾,告诉你一些位置有消防队,问各个消防队到火灾地点的最短时间,并输出最短路的路径,输出按最短时间由小到大排序。
就是一个最短路问题,输出路径,直接dijkstra了,1A还是挺爽的
#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 100010#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131#define long long ll;#define unsigned long long ull;#define lson l,m,rt<<1#define rson r,m+1,rt<<1|1struct node{ int id, s, time, pathl; int path[25];}ans[25];int edge[25][25];int vis[25];int dist[25];int path[25];int n;bool cmp(node x,node y){ return x.time<y.time;}void dijkstra(int s,int e){ int i, j; for(i=1;i<=n;i++){ dist[i] = edge[s][i]; } memset(vis,0,sizeof(vis)); memset(path,0,sizeof(path)); vis[s] = 1; for(i=0;i<n-1;i++){ int temp = INF, k = -1; for(j=1;j<=n;j++){ if(!vis[j]&&dist[j]<temp){ k = j; temp = dist[j]; } } if(k==-1) break; vis[k] = 1; for(j=1;j<=n;j++){ if(!vis[j]&&edge[k][j]!=INF&&dist[j]>dist[k]+edge[k][j]){ dist[j] = dist[k] + edge[k][j]; path[j] = k; } } }}int main(){ int i, j, s, e, x; scanf("%d",&n); for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&x); if(x==-1) edge[i][j] = INF; else edge[i][j] = x; } } scanf("%d",&e); int k = 0; printf("Org\tDest\tTime\tPath\n"); while(scanf("%d",&s)!=EOF){ dijkstra(s,e); ans[k].id = k + 1; ans[k].s = s; ans[k].time = dist[e]; ans[k].pathl = 1; int ll = 1; ans[k].path[0] = e; int ee = e; while(path[ee]!=0){ ans[k].path[ll++] = path[ee]; ee = path[ee]; ans[k].pathl++; } ans[k].path[ll++] = s; ans[k].pathl++; if(ans[k].pathl==2&&ans[k].path[0]==ans[k].path[1]) ans[k].pathl = 1; k++; } sort(ans,ans+k,cmp); for(i=0;i<k;i++){ printf("%d\t%d\t%d\t",ans[i].s,e,ans[i].time); for(j=ans[i].pathl-1;j>0;j--){ printf("%d\t",ans[i].path[j]); } printf("%d\n",ans[i].path[j]); } printf("\n"); return 0;}/*60 3 4 -1 -1 -1-1 0 4 5 -1 -12 3 0 -1 -1 28 9 5 0 1 -17 2 1 -1 0 -15 -1 4 5 4 05 5*/
0 0
- 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! (最短路+打印路径)
- ZPJ - 1053 FDNY to the Rescue!(最短路 Dijkstra),POJ 1122
- 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 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
- 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! 已被翻译
- ZOJ1053-FDNY to the Rescue!(图论之最短路)
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Python查找文件内容
- linux 常用查看系统配置命令
- 虚函数相关问题分析
- Spark Web界面
- POJ--1122--FDNY to the Rescue!【最短路】
- 各种操作系统上的操作系统日志位置
- Unable to instantiate Action, MenuAction, defined for 'QueryMenuAll' in namespace '/'MenuAction
- TransactionSynchronizationManager理解
- 基于MFC对话框的qq游戏连连看外挂
- 出现如下这个错误
- 武汉华嵌-嵌入式培训专家笔试题目
- 标题 随便写阿什利的客服经理开始的减肥死了快捷方式来得快
- 利用VMProtect sdk和ASProtect sdk加密delphi程序