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 ...
随机推荐
- 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案
安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径“C:\app\用户名\product\11.2 ...
- [51NOD1537] 分解(递推,矩阵快速幂)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1537 思路:一开始用二项式拆了一下,发现这个式子的形式总能变成 ...
- JavaScript Replace 多个字符
<html> <head> <title></title> <script language="javascript"> ...
- FJNU 1152 Fat Brother And Integer(胖哥与整数)
FJNU 1152 Fat Brother And Integer(胖哥与整数) Time Limit: 1000MS Memory Limit: 257792K [Description] [题 ...
- 1.mybatis简介
mybatis简介 MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为My ...
- elcipse 中利用maven创建web工程
如何创建: http://huxiaoheihei.iteye.com/blog/1766986 遇到的问题: 1: 如果spring MVC配置了 <servlet> <servl ...
- Nginx基础知识————生成自签名ca 证书 使nginx 支持https
创建服务器私钥,命令会让你输入一个口令: $ openssl genrsa -des3 -out server.key 1024 创建签名请求的证书(CSR): $ openssl req -new ...
- maven常见问题问答
1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把project1项目完全迁移并应用maven ...
- 如何查看与刷新DNS本地缓存
如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...
- Android LayoutInflater 动态地添加删除View
我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...