FDNY to the Rescue!

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2917   Accepted: 896

Description

The Fire Department of New York (FDNY) has always been proud of their response time to fires in New York City, but they want to make their response time even better. To help them with their response time, they want to make sure that the dispatchers know the 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

Line 1: 
# 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

Line 1: 
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 题目大意:求各个消防站到着火点的最短距离,按照距离短的顺序依次输出该消防点的距离,路径 思路:我们可以把着火点看做源点,消防站看做其他点,然后把题目给的边的距离反向构图,用迪杰斯特拉算法求最短路径 这样求出来的就是各个消防站到着火点的距离了 代码如下:
 #include <iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxs = ;
const int INF = 0x3f3f3f3f;
int N;
int edge[maxs][maxs];
int path[maxs],dist[maxs];
int start,e[maxs];
void dijkstra(int v)
{
bool vis[maxs];
memset(vis,false,sizeof(vis));
for(int i=;i<=N;i++)
{
dist[i]=edge[v][i];
if(edge[v][i]<INF)
path[i]=v;
else
path[i]=-;
}
vis[v]=true;path[start]=;
for(int i=;i<=N;i++)
{
int mins = INF,k=v;
for(int j=;j<=N;j++)
if(!vis[j]&&dist[j]<mins)
{
mins = dist[j];
k=j;
}
vis[k]=true;
for(int j=;j<=N;j++)
if(!vis[j]&&dist[j]>dist[k]+edge[k][j])
{
dist[j]=dist[k]+edge[k][j];
path[j]=k;
}
} } int main()
{
freopen("in.txt","r",stdin);
scanf("%d",&N);
memset(edge,,sizeof(edge));
int d;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
{
scanf("%d",&d);
if(d==-)
edge[j][i]=INF;//反向存储
else
edge[j][i]=d;
}
scanf("%d",&start);
d=;
while(scanf("%d",&e[++d])!=EOF);
printf("Org\tDest\tTime\tPath\n");
dijkstra(start); bool vis[maxs];
memset(vis,false,sizeof(vis));
for(int i=;i<d;i++)
{
int temp = INF,k;
for(int j=;j<d;j++)
if(dist[e[j]]<=temp&&!vis[j])
{
temp=dist[e[j]];
k=j;
}
vis[k]=true;
printf("%d\t%d\t%d\t",e[k],start,dist[e[k]]);
//打印路径
int t = e[k];
if(path[t]==-)
printf("%d\n",e[k]);
else
{
while(path[t]!=)
{
printf("%d\t",t);
t=path[t];
}
printf("%d\n",start);
}
}
return ;
}
 

poj1122的更多相关文章

  1. ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)

    两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ ...

  2. poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

    题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...

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

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

  4. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  5. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  6. 2017年暑假ACM集训日志

    20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...

随机推荐

  1. PhpStorm 注册相关

    网址 http://idea.lanyus.com/ 最新(2017年9月)PhpStorm 2017.3 .WebStorm 2017.2.5.PyCharm  2016.3激活方式 打开网址 ht ...

  2. 深入浅出 JMS(三) - ActiveMQ 安全机制

    深入浅出 JMS(三) - ActiveMQ 安全机制 一.认证 认证(Authentication):验证某个实体或者用户是否有权限访问受保护资源. MQ 提供两种插件用于权限认证: (一).Sim ...

  3. oracle两个客户端路径记录

    32 C:\WINDOWS\assembly\GAC_64\Oracle.DataAccess\2.112.3.0__89b483f429c4734264 C:\WINDOWS\Microsoft.N ...

  4. 硬件GPIO,UART,I2C,SPI电路图

  5. java读取网页图片路径并下载到本地

    java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...

  6. 2018.09.27 codeforces1045D. Interstellar battle(期望dp)

    传送门 一道有意思的期望dp. 题意是给出一棵树,每个点最开始都有一个gg的概率,有m次修改,每次修改会把某个点gg的概率更换掉,让你求出每次修改之后整个树被分成的连通块的数量的期望(gg掉的点不算) ...

  7. BSD Socket (java)

    服务器 import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java ...

  8. 2、WindowManager源码分析--最后一战

    在最后一站中,几乎所有的UI界面都是这个WindowManager管理的,那么他是如何调度的呢?我们来看看一些项目中的界面. 上面有登陆界面,专门管理登陆.战斗界面,用户界面,专门管理用户属性等等. ...

  9. ORBSlam with ROS

    ...相机标定 calibration 基本就是做CV 的常识 ORBSlam源码:

  10. ACM STEPS——Chapter Two——Section One

    数学题小关,做得很悲剧,有几道题要查数学书... 记下几道有价值的题吧 The area(hdoj 1071) http://acm.hdu.edu.cn/showproblem.php?pid=10 ...