POJ 1122 FDNY to the Rescue!
给出某些交叉点的距离,-1 表示无法到达。
然后给出火灾发生点 和 附近的消防局位置。
排列消防局 的 时间 与路径。
反向建图,以火灾出发点为起点做一次SPFA。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int n;
struct lx
{
int v,t;
};
vector<lx>g[21];
bool vis[21];
int path[21];
int dis[21];
void SPFA(int start)
{
for(int i=1;i<=n;i++)
dis[i]=INF,vis[i]=0,path[i]=0;
queue<int>q;
vis[start]=1,dis[start]=0;
q.push(start);
while(!q.empty())
{
int u=q.front();q.pop();
vis[u]=0;
for(int j=0;j<g[u].size();j++)
{
int v=g[u][j].v;
int t=g[u][j].t;
if(dis[v]>dis[u]+t)
{
dis[v]=dis[u]+t;
path[v]=u;// 记录路径
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
struct node
{
int Org,time;
}l[21];
bool cmp(node a,node b)
{
return a.time<b.time;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int t;
scanf("%d",&t);
if(i==j||t==-1)continue;
lx now;
now.v=i,now.t=t;
g[j].push_back(now);
}
}
int start,endcot=0;
int thend[21];
scanf("%d",&start);
getchar();
char str[101];
gets(str);
int i=0;
int c=0;
while(str[i]==' ')i++;
for(i=0;i<=strlen(str);i++)
{
while(str[i]>='0'&&str[i]<='9')c=c*10+str[i]-'0',i++;
if(c!=0)
thend[endcot++]=c,c=0;
}
SPFA(start);
for( i=0;i<endcot;i++)
l[i].Org=thend[i],l[i].time=dis[thend[i]];
sort(l,l+endcot,cmp);
puts("Org Dest Time Path");
for( i=0;i<endcot;i++)
{
int v=l[i].Org;
printf("%d %d %d",v,start,dis[v]);
int tmp=v;
while(path[tmp]!=0)
printf(" %d",tmp),tmp=path[tmp];
printf(" %d\n",start); }
}
}
POJ 1122 FDNY to the Rescue!的更多相关文章
- POJ 1122.FDNY to the Rescue! Dijkstra
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2808 Accepted: 86 ...
- POJ 1122 FDNY to the Rescue!(最短路+路径输出)
http://poj.org/problem?id=1122 题意:给出地图并且给出终点和多个起点,输出从各个起点到终点的路径和时间. 思路: 因为有多个起点,所以这里反向建图,这样就相当于把终点变成 ...
- POJ 1122 FDNY to the Rescue! Floyd 打印路径就行了
题目大意: 纽约消防部门的支援速度是值得纽约人骄傲的一件事.但是他们想要最快的支援速度,帮助他们提升支援速度他们要调度离着火点最近的一个消防站.他们要你写一个程序来维护纽约消防站的光荣传统.软件需要有 ...
- poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- POJ1122_FDNY to the Rescue!(逆向建图+最短路树)
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2368 Accepted: 72 ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
随机推荐
- 刘德华夏日Fiesta演唱会上那个表演探戈舞的演员是谁啊?_百度知道
刘德华夏日Fiesta演唱会上那个表演探戈舞的演员是谁啊?_百度知道 刘德华夏日Fiesta演唱会上那个表演探戈舞的演员是谁啊? 2008-05-28 00:04 topofhill | ...
- 项目.c文件和.h文件关系
"如有不正确之处,请指出,谢谢" --Mood 对于一个项目,我们应该要很好的处理众多的.c和.h文件. 1.通过头文件调用库功能:#inclu ...
- java调用C#的dll
链接地址:http://www.cnblogs.com/yinhaiming/articles/1712463.html .net产生的比java晚,其类库的封装在某些方面也比java更优秀,更全面. ...
- C#加密与解密
密码加密之类的用处,直接复制黏贴,可用 1.加密的代码: /// <summary> /// DEC 加密过程 /// </summary> /// <param nam ...
- poj 2299 逆序数
http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...
- HTML5 input placeholder 颜色 改动
David Murdoch:Chrome支持input=[type=text]占位文本属性,但下列CSS样式却不起作用: CSS input[placeholder], [placeholder], ...
- 【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 UI在游戏中占有很重要的地位,但吸引玩家的除了这 ...
- UVA它11292 - Dragon of Loowater
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- PreTranslateMessage和TranslateMessage区别
PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当需要在MFC之前处理某些消息时,常常要在这里添加代码. ...
- Metasploit学习之msf连接数据库
kali使用metasploit开启数据服务: 首先,初次使用系统要初始化建立数据库msf3, 否则的话 /opt/metasploit/apps/pro/ui/config/databse.yml不 ...