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. DNS/BIND in Debian

    Debian official document:http://www.debian.org/doc/manuals/network-administrator/ch-bind.html Buildi ...

  2. CentOs7中的网卡配置工具

    CentOs7中的网卡配置工具 摘自:https://blog.51cto.com/13572810/2087991 misslaziness1人评论2715人阅读2018-03-17 22:09:1 ...

  3. Devexpress VCL Build v2014 vol 14.2.7发布

    2015年马上快过去一半了,这个玩意还在纠结在14版.其实也无所谓,反正就是改成15版,也还是这些 东西的修补. What's New in 14.2.7 (VCL Product Line)   N ...

  4. 浅析10种常见的黑帽seo手法

    虽然博主并不认同黑帽seo手法,但是一些常见的黑帽手法还是需要了解的,增加自己对黑帽的认知,也可以在自己优化网站时适时的规避开这些黑帽手法,从而避免自己的网站被搜索引擎惩罚.好了,话不多说,下面进入今 ...

  5. 2018.08.17 洛谷[POI2010]GRA-The Minima Game(线性dp)

    传送门 短代码神奇dp. 自己yy的思路居然1A了好高兴啊! 不难想到每个人选择的时候一定是取连续的最大的那一段数,自然需要先排序. 然后可以用dp[i]表示当前最大数是a[i]的时候先手可以获得的最 ...

  6. Netty学习第五节实例进一步学习

    概念理解: Netty是基于NIO的框架  传统IO与NIO的区别:       1.传统IO会造成阻塞点:       2.单一的客户端处理消息 解决阻塞问题:建立线程池,达到收到一个消息就建立一个 ...

  7. chandy-lamport 分布式一致性快照 算法详细介绍

    在一个分布式计算系统中,为了保证数据的一致性需要对数据进行一致性快照.Flink和spark在做流失计算的时候都借鉴了chandy-lamport算法的原理,这篇文章就是对chandy-lamport ...

  8. b4和tncl_extract_UNCL_new

    # -*- coding:utf-8 -*- import re ''' 适应新版本 注意: 1)17A文件改完后缀后,需要转为UTF-8无BOM格式,才能正确处理. 2)fr = open(file ...

  9. (最小生成树)Jungle Roads -- HDU --1301

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. Android操作HTTP实现与服务器通信

    (转自http://www.cnblogs.com/hanyonglu/archive/2012/02/19/2357842.html) 本示例以Servlet为例,演示Android与Servlet ...