POJ 1122 反向建图Dijkstra+记录路径 输入格式坑
2016-08-06 18:01
288 查看
FDNY to the Rescue!
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2897 Accepted: 887
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
题目大意:在一个地图上,找出从消防站到着火点的最短时间与最短时间中所走的点
只有一组数据
第一行代表一张地图,有n个点
接着是一个n×n的矩阵,代表从第i行j列中,i到j的时间,-1代表不能到达
最后一行,第一个数代表着火点,后面多个数代表消防站
输出的是消防站 着火点 时间 路径
解题思路:
这题要处理建图的关系,虽然图是无向图,但i到j的时间并不是j到i的时间,灭火大队又要从消防站出发到达火源,而火源是固定的,所以逆向建图是最好的处理,找从火源出发到达消防站的最短时间。就可以求出从所有点到着火点的最短路径,在算最短路径的时候记录路径点,这样从时间小的输出即可
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2897 Accepted: 887
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
题目大意:在一个地图上,找出从消防站到着火点的最短时间与最短时间中所走的点
只有一组数据
第一行代表一张地图,有n个点
接着是一个n×n的矩阵,代表从第i行j列中,i到j的时间,-1代表不能到达
最后一行,第一个数代表着火点,后面多个数代表消防站
输出的是消防站 着火点 时间 路径
解题思路:
这题要处理建图的关系,虽然图是无向图,但i到j的时间并不是j到i的时间,灭火大队又要从消防站出发到达火源,而火源是固定的,所以逆向建图是最好的处理,找从火源出发到达消防站的最短时间。就可以求出从所有点到着火点的最短路径,在算最短路径的时候记录路径点,这样从时间小的输出即可
#include<iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn = 25; const int INF = 99999999; int map[maxn][maxn],dis[maxn],lujing[maxn]; int n,fire,sum;//sum为消防站数量 int Is_station[maxn]; void dijkstra(){ int i,j,v,tmp; bool vis[maxn]; memset(lujing,-1,sizeof(lujing)); memset(vis,false,sizeof(vis)); for(i=1;i<=n;i++) dis[i]=INF; dis[fire]=0; for(i=1;i<=n;i++){ tmp=INF; for(j=1;j<=n;j++) if(dis[j]<tmp && !vis[j]){ tmp=dis[j]; v=j; } vis[v]=true; //这里是求火源到消防站的路径,在输出时,当lujing[j],j是消防站的位置,则倒着输出位置直到火源 for(j=1;j<=n;j++){ if(!vis[j] && map[v][j]!=-1 && dis[j]>dis[v]+map[v][j]){ //-1不能走 dis[j]=dis[v]+map[v][j]; lujing[j]=v; //记录路径 } } } } void output(){ int j,i,tmp,v; printf("Org\tDest\tTime\tPath\n"); for(int j = 0;j<sum;j++){ tmp=INF; for(i=1;i<=n;i++){ //每次找最小的时间输出 if(Is_station[i] && dis[i]<tmp){ tmp=dis[i]; v=i; } } Is_station[v]=0; //将这个站点设为非消防站 printf("%d\t%d\t%d\t",v,fire,dis[v]); while(v!=-1){ printf("%d\t",v); v=lujing[v]; } printf("\n"); } } int main(){ int i,j,q; scanf("%d",&n); for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&map[j][i]); //因为火源固定,边反向存储方便处理 } } scanf("%d",&fire); sum=0; memset(Is_station,0,sizeof(Is_station)); while(~scanf("%d",&q)) { Is_station[q]=1; //标记消防站 sum++; //计算消防站总数 } dijkstra(); output(); return 0; }
相关文章推荐
- dijkstra spfa记录最短路径路线
- uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交
- poj2457 Part Acquisition(dijkstra||spfa+路径记录优化)
- L2-001. 紧急救援---(Dijkstra,记录路径)
- PTA 5-7 城市间紧急救援 【Dijkstra+记录路径】
- 用格式输出函数输入三个字符,并用输出函数反向输出三个字符和他们的ASCII码
- 八数码第四境界——暴力反向BFS+康托展开判重+打表+回溯记录路径
- poj 1122 dijkstra+记录路径
- L2-001. 紧急救援(dijkstra+记录路径)
- 20170813(两道题-dijkstra记录路径 BFS)
- 2、 excel 导出。这种输入方式,适合后台输出,不需要打开EXCEL文件,不需要选择输出路径,只是后台自己默默的记录EXCEL文件。
- openCV打开图像或视频时绝对路径输入格式
- POJ 2457 Part Acquisition (Dijkstra + 记录路径)
- dijkstra求最短路并记录路径
- MapReduce深入理解输入格式(1)-输入分片与记录
- HDU ACM 3986 Harry Potter and the Final Battle(邻接表实现最短路dijkstra堆优化记录路径 + 枚举最短路上每条边)
- hdu1595 find the longest of the shortest (dijkstra+记录路径)
- 最短路径算法 dijkstra + floyd + spfa 【记录 总结】
- Dijkstra算法-单源最短路径(包括路径记录)
- 插入数据库记录时候“输入字符串的格式不正确。 ”