POJ1122_FDNY to the Rescue!(逆向建图+最短路树)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2368 | Accepted: 721 |
Description
closest firehouse to any address in the city. You have been hired to write this software and are entrusted with maintaining the proud tradition of FDNY. Conceptually, the software will be given the address of the fire, the locations of the firehouses, street
intersections, and the time it takes to cover the distance between each intersection. It will then use this information to calculate how long it takes to reach an address from each firehouse.
Given a specific fire location in the city, the software will calculate the time taken from all the fire stations located in the city to reach the fire location. The list of fire stations will be sorted from shortest time to longest time. The dispatcher can
then pick the closest firestation with available firefighters and equipment to dispatch to the fire.
Input
# of intersections in the city, a single integer (henceforth referred to as N) N<20
Lines 2 to N+1:
A table (square matrix of integer values separated by one or more spaces) representing the time taken in minutes between every pair of intersections in the city. In the sample input shown below the value "3" on the 1st row and the 2nd column represents the
time taken from intersection #1 to reach intersection #2.
Similarly the value "9" on the 4th row and the 2nd column represents the time taken from intersection #4 to reach intersection #2.
A value of -1 for time means that it is not possible to go directly from the origin intersection (row #) to the destination intersection (column #). All other values in the table are non-negative.
Line N+2:
An integer value n (<= N) indicating the intersection closest to the fire location followed by one or more integer values for the intersections closest to the fire stations (all on one line, separated by one or more spaces) will follow the input matrix.
Notes on input format:
1. The rows and columns are numbered from 1 to N.
2. All input values are integers
3. All fire locations are guaranteed reachable from all firehouses.
4. All distance calculations are made from the intersection closest to each firehouse to the intersection closest to the fire.
Output
A label line with the headings for each column, exactly as shown in the example.
Line 2 onwards (one line for each fire station):
A sorted list (based on time) showing the fire station (origin), the destination site, time taken and a complete shortest path of nodes from the originating fire station to the fire location.
Notes on output format:
1. Columns are tab separated.
2. If two or more firehouses are tied in time they can be printed in any order.
3. If more than one path exists that has the same minimal time for a given location & firehouse, either one can be printed on the output.
4. If the fire location and the fire station locations happen to be the same intersection, the output will indicate that the origin and destination have the same intersection number, the time will be "0" and the nodes in the shortest path will show just one
number, the fire location.
Next is the picture for the sample input data.

Sample Input
6
0 3 4 -1 -1 -1
-1 0 4 5 -1 -1
2 3 0 -1 -1 2
8 9 5 0 1 -1
7 2 1 -1 0 -1
5 -1 4 5 4 0
2 4 5 6
In the above input the last line indicates that "2" is the location of the fire and "4", "5" and "6" are the intersections where fire stations are located.
Sample Output
Org Dest Time Path
5 2 2 5 2
4 2 3 4 5 2
6 2 6 6 5 2
Source
。(心烦是不是下午模拟邀请赛挂零了。。。
)
。
。
。
。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 99999999
#define Max 30
using namespace std;
int n,mmap[Max][Max],dis[Max],vis[Max],pre[Max];
struct node
{
int org,dest,time;
} num[Max];
int cmp(node a,node b)
{
return a.time<b.time;
}
void dij(int s)
{
int i,j,u,minn;
for(i=1; i<=n; i++)
{
dis[i]=mmap[s][i];
vis[i]=0;
if(i!=s)
pre[i]=s;
else pre[i]=-1;
}
dis[s]=0;
vis[s]=1;
for(i=0; i<n-1; i++)
{
u=0;
minn=inf;
for(j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
vis[u]=1;
for(j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]>dis[u]+mmap[u][j])
{
dis[j]=dis[u]+mmap[u][j];
pre[j]=u;
}
}
}
}
int main()
{
int i, j, s, u, cnt=0;
cin>>n;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d",&mmap[j][i]);
if(mmap[j][i]==-1)
mmap[j][i]=inf;
}
}
cin>>s;
dij(s);
while(cin>>u)
{
num[cnt].org=u;
num[cnt].dest=s;
num[cnt++].time=dis[u];
}
printf("Org\tDest\tTime\tPath\n");
int shortest[Max];
sort(num,num+cnt,cmp);
for(i=0; i<cnt; i++)
{
printf("%d\t%d\t%d",num[i].org,num[i].dest,num[i].time);
memset(shortest,0,sizeof(shortest));
int k=0;
shortest[k]=num[i].org;
while(pre[shortest[k]]!=-1)
{
k++;
shortest[k]=pre[shortest[k-1]];
}
// k++;
// shortest[k]=s;
for(j=0;j<=k;j++)
printf("\t%d",shortest[j]);
printf("\n");
}
return 0;
}
POJ1122_FDNY to the Rescue!(逆向建图+最短路树)的更多相关文章
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- Invitation Cards(邻接表+逆向建图+SPFA)
Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 17538 Accepted: 5721 Description In ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- hdu 4857 逆向建图+拓扑排序 ***
题意:糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行.现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有 ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 逃生 HDU 4857(反向建图 + 拓扑排序)
逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...
- poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...
- PIGS POJ - 1149网络流(最短增广路---广搜) + 建图
题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...
随机推荐
- 解决win10下微信开发者工具点击错位问题
在系统设置->显示->更改文本.应用等项目的大小选项中将百分比改为100%即可.
- Codeforces Round #446 (Div. 2) A. Greed【模拟】
A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- sort equal 确保记录按照 input顺序来
Usually you have a requirement of removing the duplicate records from a file using SORT with the opt ...
- 磁盘镜像工具Guymager
磁盘镜像工具Guymager 在数字取证中,经常需要对磁盘制作镜像,以便于后期分析.Kali Linux提供一款轻量级的磁盘镜像工具Guymager.该工具采用图形界面化方式,提供磁盘镜像和磁盘克 ...
- js获取屏幕
js获取屏幕(设备)宽高 <script language="javascript"> var h = ""; h += " 网页可见区域 ...
- [POJ 2397] Spiderman
Link: POJ 2397 传送门 Solution: 设$dp[i][j]$表示第$i$步走到$j$高度时经过的最高高度 分向上走和向下走两种方式转移即可 注意记录路径,最后输出时要逆序输出 (逆 ...
- 代理模式(Proxy)--动态代理(JDK)
在是上一篇博客中实现了静态代理. 在上篇的结尾提到了一个问题: 思考:如果我们下需要对火车,自行车实现相同的代理,我们又该如何实现呢? 这篇博客就来解决这个问题: 解决这类问题需要用到动态代理技术,实 ...
- iOS duplicate symbol for architecture arm64 解决办法
导致这个问题的原因有多种: 1.重复定义了const常量. 2.多个第三方库同时用到了某个函数库. 暂时列举这几种,以后遇到了其他原因再加.
- C#如何把日期转成YYYYMMDDHHMMSSFFF的精确到毫秒的格式?
C#如何把YYYY-MM-DD HH:MM:SS格式的日期转成YYYYMMDDHHMMSS的格式? 方法一:string src= (new DataTime()).ToString(); str ...
- Android 使用SQLite本地数据库
参考:http://blog.csdn.net/jianghuiquan/article/details/8569252 在Android平台上,集成了一个嵌入式关系型数据库—SQLite.以SQLi ...