题目链接:poj1122 FDNY to the Rescue!

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

题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std; const int inf = 0x3f3f3f3f;
const int N = ;
int n, fire;
int g[N][N];//邻接矩阵存图
int s[N], d[N];
int path[N];//表示v0到vi最短路径顶点vi的前一个顶点序号
int shortest[N];//存储最短路径上的各个顶点序号
struct node{
int u, v, t;
}a[N];
int cmp(node a, node b){
return a.t < b.t;
}
void dij(){
int i, j, k;
CLR(s, );
for(i = ; i <= n; ++i){
d[i] = g[fire][i];
if(i != fire)
path[i] = fire;
else
path[i] = -;
}
s[fire] = ;
d[fire] = ;
for(i = ; i < n-; ++i){
int mi = inf, u = ;
for(j = ; j <= n ; ++j){
if(!s[j] && d[j] < mi){
u = j; mi = d[j];
}
}
s[u] = ;
for(k = ; k <= n; ++k){
if(!s[k] && d[u] + g[u][k] < d[k]){
d[k] = d[u] + g[u][k];
path[k] = u;
}
}
}
}
int main(){
int i, j, x, cnt;
scanf("%d", &n);
for(i = ; i <= n; ++i){//边反向存储
for(j = ; j <= n; ++j){
scanf("%d", &x);
if(x == -)
g[j][i] = inf;
else
g[j][i] = x;
}
}
scanf("%d", &fire);
dij();
cnt = ;
while(scanf("%d", &x) == ){
a[cnt].u = x;
a[cnt].v = fire;
a[cnt++].t = d[x];
}
sort(a, a+cnt, cmp);
printf("Org\tDest\tTime\tPath\n");
for(i = ; i < cnt; ++i){
printf("%d\t%d\t%d", a[i].u, a[i].v, a[i].t);
CLR(shortest, );
int k = ;//表示shortest数组中最后一个元素的下标
shortest[k] = a[i].u;
while(path[shortest[k]] != -){
k++;
shortest[k] = path[shortest[k-]];
}
for(j = ; j <= k; ++j) //倒向追踪,但是反向建图,因此正向输出
printf("\t%d", shortest[j]);
puts("");
}
return ;
}

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)的更多相关文章

  1. POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图

    题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...

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

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

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

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

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

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

  5. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  6. HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...

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

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

  8. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. CodeForces - 350B(反向建图,)

    B - Resort CodeForces - 350B B. Resort time limit per test 2 seconds memory limit per test 256 megab ...

随机推荐

  1. HTTP Status 404–/webDemo/hello

    现在用一排很小的字写出来,我真是个大傻逼

  2. eclipse下添加viplugin插件的方法

    http://www.viplugin.com/ 在eclipse根目录下建立文件:viplugin2.lic,然后在里面添加以下字符串: nd4UFjUMBADcUSeSW8ocLKoGP3lpbW ...

  3. 4、Type fundamentals

    1.All Types Are Derived from System.Object The CLR requires all objects to be created using the new ...

  4. [C和指针]第四部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. CUBRID学习笔记 14 dll加载错误

    这个问题通常是缺少文件cascci.dll 或者版本错误 32 64弄错了 C:\Program Files (x86)\Python266>python.exe Python 2.6.6 (r ...

  6. IBM Lotus Domino V8.5 服务器管理入门手册

    转自 http://freemanluo.blog.51cto.com/636588/336128

  7. [转] Java内部类详解

    作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...

  8. iOS - Swift Range 范围

    前言 Range:结构体,这个结构体用来表示一个区间的范围. public struct Range<Element : ForwardIndexType> : Equatable, Co ...

  9. Redis基础知识之————如何处理客户端连接

    redis 连接建立 Redis Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作: 首先,客户端 ...

  10. mac10.12的Cocopods安装使用

    Cocopods的安装 CocoaPods应该是iOS最常用最有名的类库管理当我们开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等,可能某个类库又用到其他的库,手动一 ...