poj1122

              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

题目大意:求各个消防站到着火点的最短距离,按照距离短的顺序依次输出该消防点的距离,路径

思路:我们可以把着火点看做源点,消防站看做其他点,然后把题目给的边的距离反向构图,用迪杰斯特拉算法求最短路径

这样求出来的就是各个消防站到着火点的距离了

代码如下:
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5
 6 const int maxs = 30;
 7 const int INF = 0x3f3f3f3f;
 8 int N;
 9 int edge[maxs][maxs];
10 int path[maxs],dist[maxs];
11 int start,e[maxs];
12 void dijkstra(int v)
13 {
14     bool vis[maxs];
15     memset(vis,false,sizeof(vis));
16     for(int i=1;i<=N;i++)
17     {
18         dist[i]=edge[v][i];
19         if(edge[v][i]<INF)
20             path[i]=v;
21         else
22             path[i]=-1;
23     }
24     vis[v]=true;path[start]=0;
25     for(int i=1;i<=N;i++)
26     {
27         int mins = INF,k=v;
28         for(int j=1;j<=N;j++)
29             if(!vis[j]&&dist[j]<mins)
30             {
31                 mins = dist[j];
32                 k=j;
33             }
34         vis[k]=true;
35         for(int j=1;j<=N;j++)
36             if(!vis[j]&&dist[j]>dist[k]+edge[k][j])
37             {
38                 dist[j]=dist[k]+edge[k][j];
39                 path[j]=k;
40             }
41     }
42
43 }
44
45 int main()
46 {
47     freopen("in.txt","r",stdin);
48     scanf("%d",&N);
49     memset(edge,0,sizeof(edge));
50     int d;
51     for(int i=1;i<=N;i++)
52         for(int j=1;j<=N;j++)
53         {
54             scanf("%d",&d);
55             if(d==-1)
56                 edge[j][i]=INF;//反向存储
57             else
58                 edge[j][i]=d;
59         }
60     scanf("%d",&start);
61     d=0;
62     while(scanf("%d",&e[++d])!=EOF);
63     printf("Org\tDest\tTime\tPath\n");
64     dijkstra(start);
65
66     bool vis[maxs];
67     memset(vis,false,sizeof(vis));
68     for(int i=1;i<d;i++)
69     {
70         int temp = INF,k;
71         for(int j=1;j<d;j++)
72             if(dist[e[j]]<=temp&&!vis[j])
73             {
74                 temp=dist[e[j]];
75                 k=j;
76             }
77         vis[k]=true;
78         printf("%d\t%d\t%d\t",e[k],start,dist[e[k]]);
79         //打印路径
80         int t = e[k];
81         if(path[t]==-1)
82             printf("%d\n",e[k]);
83         else
84         {
85             while(path[t]!=0)
86             {
87                 printf("%d\t",t);
88                 t=path[t];
89             }
90             printf("%d\n",start);
91         }
92     }
93     return 0;
94 }
 
时间: 2024-10-09 13:41:00

poj1122的相关文章

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

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

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

两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ1122)-FDNY to the Rescue! 1 //POJ1122-ZOJ1053 2 //dijkstra-需要记录路径 3 //给出n个路口的邻接矩阵,求给定多个火警到失火点的时间及任一路径 4 //注意输入最后一行时,cin.getline需要两次,猜测需要接受邻接矩阵最后一行其他字符

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,