FDNY to the Rescue!
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2175 | Accepted: 655 |
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.
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.
# 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.
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
这是一道在有向图上找最短路径的题目,而且是要输出多个路径,一开始实在是想不出来,搜索题意的时候,看到大牛的思想,就是做个反向的dijkstra,是个多起点,当终点的问题。提议大概就是消防员要去救火,有多个救火站,要分别打出各个救火站到火灾现场的最近的时间,并按从小到大排序。并输出路上经过的十字路口。
下面是代码:
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #define max 50 #define inf 0x7FFFFFFF # define eps 1e-5 using namespace std; int edge[max][max],index[max];//边的信息,访问,记录路径 int n,m,v0; int visit[max]; struct Dist { int tim; int vis; int num; } dist[max]; bool cmp(Dist a,Dist b)//用于sort时的比较函数 { return a.tim<b.tim; } void print(int u0) { printf("%d\t",u0); if(index[u0]!=-1) print(index[u0]); } void Dijkstra(int u0) { int i,j,v; memset(visit,0,sizeof(visit)); visit[u0]=1; index[u0]=-1; for(j=0; j<n-1; j++) { int min=inf; for(i=1; i<=n; i++) { if(!visit[i]&&min>dist[i].tim) { min=dist[v=i].tim; } } visit[v]=1; for(i=1; i<=n; i++) { if(!visit[i]&&edge[i][v]!=inf&&edge[i][v]+dist[v].tim<dist[i].tim) { dist[i].tim=dist[v].tim+edge[i][v]; index[i]=v;//这是一个关键点,记录了i的前驱结点v } } } } int main() { int i,j,u0,r[max],t=0; char x[1000];//一开始想的就是用int型数组,后来发现不行 scanf("%d",&n); for(i=1; i<=n; i++) for(j=1; j<=n; j++) { scanf("%d",&edge[i][j]); if(edge[i][j]==-1) edge[i][j]=inf; } scanf("%d",&v0); for(i=1; i<=n; i++) { dist[i].tim=edge[i][v0]; dist[i].vis=0; dist[i].num=i; index[i]=v0; } gets(x);//因为后面的消防站无法固定,所以不能用scanf,只能用gets int len=strlen(x);//这就是用char的原因,因为为了后面的循环,必须知道数组的长度,所以用字符数组,通过strlen函数,求得数组长度 for(i=0; i<len; i++) { if(x[i]>='0'&&x[i]<='9') { r[t]=x[i]-'0'; dist[r[t]].vis=1; } } Dijkstra(v0); sort(dist+1,dist+n+1,cmp); printf("Org\tDest\tTime\tPath\n"); for(i=1; i<=n; i++) { if(dist[i].vis==1) { printf("%d\t%d\t%d\t",dist[i].num,v0,dist[i].tim); print(dist[i].num); printf("\n"); } } return 0; }