题目链接:

https://vjudge.net/problem/POJ-1122

题目大意:

给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置。输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口。

思路:

由于求的是其他点到某一点的最短路,所以反向建图,然后用dijkstra求出这点到其他点的最短路即可。求出来的就是其他点到这点的最短路,还需要保存路径。输入输出有格式要求,详细看代码

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<sstream>
#define MEM(a, b) memset(a, b, sizeof(a));
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int T, n, m, cases, tot;
int Map[maxn][maxn], d[maxn], path[maxn];
bool v[maxn];
struct node
{
int id, time;
bool operator < (const node& a)const
{
return time < a.time;
}
}a[maxn];
void dijkstra(int u)
{
for(int i = ; i <= n; i++)d[i] = INF;
d[u] = ;
memset(path, -, sizeof(path));
memset(v, , sizeof(v));
for(int i = ; i < n; i++)
{
int x, m = INF;
for(int i = ; i <= n; i++)if(!v[i] && m > d[i])m = d[x = i];
v[x] = ;
for(int i = ; i <= n; i++)
{
if(!v[i] && d[i] > d[x] + Map[x][i])
{
d[i] = d[x] + Map[x][i];
path[i] = x;
}
}
}
}
int main()
{
cin >> n;
int tot = ;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
cin >> Map[i][j];
if(Map[i][j] == -)Map[i][j] = INF;
}
}
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)swap(Map[i][j], Map[j][i]);
}
string s;
int u, v;
cin >> u;
while(cin >> v)a[tot++].id = v;
dijkstra(u);
for(int i = ; i < tot; i++)
{
a[i].time = d[a[i].id];
}
sort(a, a + tot);
printf("Org\tDest\tTime\tPath\n");
for(int i = ; i < tot; i++)
{
printf("%d\t%d\t%d\t", a[i].id, u, a[i].time);
int x = a[i].id;
while(x != -)
{
printf("%d\t", x);
x = path[x];
}
printf("\n");
}
return ;
}

POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图的更多相关文章

  1. poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

    题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...

  2. POJ 1122.FDNY to the Rescue! Dijkstra

    FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2808   Accepted: 86 ...

  3. POJ 1122 FDNY to the Rescue!(最短路+路径输出)

    http://poj.org/problem?id=1122 题意:给出地图并且给出终点和多个起点,输出从各个起点到终点的路径和时间. 思路: 因为有多个起点,所以这里反向建图,这样就相当于把终点变成 ...

  4. POJ 1122 FDNY to the Rescue!

    给出某些交叉点的距离,-1 表示无法到达. 然后给出火灾发生点  和 附近的消防局位置. 排列消防局 的 时间 与路径. 反向建图,以火灾出发点为起点做一次SPFA. #include<cstd ...

  5. POJ 1122 FDNY to the Rescue! Floyd 打印路径就行了

    题目大意: 纽约消防部门的支援速度是值得纽约人骄傲的一件事.但是他们想要最快的支援速度,帮助他们提升支援速度他们要调度离着火点最近的一个消防站.他们要你写一个程序来维护纽约消防站的光荣传统.软件需要有 ...

  6. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  7. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  8. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  9. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

随机推荐

  1. elasticsearch------java操作之QueryBuilders构建搜索Query

    版权声明:本文为非原创文章,出处:http://blog.csdn.net/xiaohulunb/article/details/37877435. elasticsearch 分布式搜索系列专栏:h ...

  2. java报错排解

    1.eclipse新安装第一次启动报错: Javawas started but returned exit code=13-- 这是由于JDK和eclipse和电脑的位数不一致所致,要么都为32位, ...

  3. angularJS 指令解释

    本文引自 http://blog.csdn.net/kongjiea/article/details/49840035 指令,很重要 AngularJS与jQuery最大的区别在哪里?我认为,表现在数 ...

  4. 笔记:Spring Cloud Zuul 快速入门

    Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...

  5. return false与return true的区别

    <a href="http://www.baidu.com" onclick="alert(11);return true;alert(22)">链 ...

  6. 大数据 --> MapReduce原理与设计思想

    MapReduce原理与设计思想 简单解释 MapReduce 算法 一个有趣的例子:你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座 ...

  7. [poj3254]Corn Fields_状压dp

    Corn Fields poj3254 题目大意:给你一个n*m的地,每一块地可以种或不种,两块种过的地不能挨着,可以一块都不种,问所有的种地方案数. 注释:读入用0和1,1<=n,m<= ...

  8. 为什么说android UI操作不是线程安全的

    转载于:http://blog.csdn.net/lvxiangan/article/details/17218409#t2 UI线程及Android的单线程模型原则 使用Worker线程 Commu ...

  9. 实现Windows程序的数据的绑定

    1.创建DataSet对象 语法: DataSet  数据集对象  =new  DataSet("数据集的名称字符串"); 语法中的参数是数据集的名称字符串,可以有,也可以没有.如 ...

  10. Linux下的/etc/hosts文件

    在Unix系统下面有一个/etc/hosts文件,在我的Mac上,这个文件的内容如下: ## # Host Database # # localhost is used to configure th ...