poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接: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+反向建图+输出路径)的更多相关文章
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)
http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- CodeForces - 350B(反向建图,)
B - Resort CodeForces - 350B B. Resort time limit per test 2 seconds memory limit per test 256 megab ...
随机推荐
- How do I reset Windows Update components?
https://support.microsoft.com/en-us/kb/971058 Download and run the Windows Update Troubleshooter for ...
- JAVAWEB安全开发
晚上在看司马的博客,看到一篇关于JAVA安全的,基础的,蛮不错的,给大家分享下 文章来源是司马的博客:http://www.nxadmin.com/web/1332.html ============ ...
- 转载:windows的mysql提权方式
mysql提权语句归纳如下: 一 UDF提权 这类提权方法我想大家已经知道了,我大致写一下,具体语句如下: create function cmdshell returns string soname ...
- Codeforces Round #257 (Div. 2) B
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- ubuntu_常用命令_01
1. 获取超级用户权限(需要输入超级用户的密码) sudo -i 2. 文本编辑器(暂时觉得 类似Windows里面的txt,更多功能有待发现) gedit /xxx/yyy ZC: 貌似 这个命令 ...
- mysql 锁的粒度
1.锁的类型分为读锁和写锁,这个很好区分.可以这样认为:如果有增删改,就是写锁.如果是查询,就是读锁.2.锁的粒度也就是锁的范围,分为行锁和表锁.锁的范围和多个因素有关,包括事务隔离级别.是否使用索引 ...
- 使用fragment兼容低版本的写法
[1]定义fragment继承V4包中的Fragment [2]定义的activity要继承v4包中的FragmentActivity [3]通过这个方法getSupportFragme ...
- 僵尸进程学习 & 进程状态列表 & Linux信号学习
参考这篇文章: http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/ 在Linux进程的状态中,僵尸进程 ...
- 在beforeAction里redirect无效,Yii2.0.8
我是在官方GitHub上得到回答,试了一下,确实解决问题了.之前的问题描述: 之前是2.0.3,然后用composer直接升级到2.0.8,就不正常了,以为是我代码的问题,于是再次尝试 用compos ...
- MyEclipse/Eclipse中XML文件的格式化配置
Eclipse中XML文件的格式化配置 MyEclipse: 这一步的配置是使格式化的效果为控件的每个属性配置占一行.进入 Window/Preferences,展开到 XML/XML Resourc ...