题目链接: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. [HDOJ5935]Car(精度,数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935 题意:有个老司机在开车, 开车过程中车的速度是不减的. 交警记录了这个老司机在nn个时间点的位置 ...

  2. 安装 slowhttptest ddos攻击软件

    kali:apt-get install slowhttptest

  3. MySQL子查询(SubQuery)

    由比较运算符引发的子查询,若括号内的子查询结果为多个,要在括号前加上ANY/SOME/ALL 由[NOT]IN引发的子查询, =ANY与IN等效       !=ALL / <>ALL与N ...

  4. Javascript屏蔽回车提交表单

    html利用input防止回车提交 默认情况下,单个输入框,无论按钮的type="submit"还是type="button"类型,回车即提交. 1.当type ...

  5. iOS - OC Copy 拷贝

    前言 copy:需要先实现 NSCopying 协议,创建的是不可变副本. mutableCopy:需要实现 NSMutableCopying 协议,创建的是可变副本. 浅拷贝:指针拷贝,源对象和副本 ...

  6. iOS - Swift Set 集合

    前言 Set:集合 public struct Set<Element : Hashable> : Hashable, CollectionType, ArrayLiteralConver ...

  7. RPC的学习 & gprotobuf 和 thrift的比较

    参考 http://blog.csdn.net/pi9nc/article/details/17336663 集成libevent,google protobuf的RPC框架 RPC(Remote P ...

  8. git的作用和原理(待续)

    先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...

  9. Asp.net_Webservice返回json

    [WebMethod] public List<PictureManager> LoadPictureOne() { dataDataContext context = new dataD ...

  10. jquery中DOM

    节点包裹 wrap() (1)$().wrap(html) 将选择的节点用指定的元素包装 $('p').wrap('<div></div>'); (2)多层包裹 $('p'). ...