ZOJ1053-FDNY to the Rescue!(图论之最短路)
来源:互联网 发布:广州qt场及js汇总贴吧 编辑:程序博客网 时间:2024/06/05 06:58
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 Format:
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 Format:
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input:
1
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
Source: Mid-Atlantic USA 2001
思路:最短路+路径还原;
数据较小用floy算法求最短路;
代码:
#include <bits/stdc++.h>using namespace std;const int maxN=21;const int inf=10000; int path[maxN][maxN];//记录经过的路口 int city[maxN][maxN];//邻接矩阵 int from[maxN];//消防站位置 void PRINT(int x, int y){if (path[x][y] == 0) return;PRINT(x, path[x][y]);printf("\t%d", path[x][y]);PRINT(path[x][y], y);}int main(){int T;scanf("%d", &T);while (T--){int n;scanf("%d", &n);int i, j, k;for (i = 1; i <= n; i++)for (j = 1; j <= n; j++){scanf("%d", &city[i][j]);if (city[i][j] == -1) city[i][j] = inf;}for (k = 1; k <= n; k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (city[i][k] + city[k][j] < city[i][j]){city[i][j] = city[i][k] + city[k][j];path[i][j] = k;//保存经过的路口 }int fire;scanf("%d", &fire);char c;int m = 0;//确定几个消防站 while((c = getchar()) != '\n')scanf("%d", &from[++m]);for (i = 1; i <= m-1; i++)for (j = i+1; j <= m; j++)if (city[from[i]][fire] > city[from[j]][fire]) swap(from[i], from[j]);//消防站位置交换 printf("Org\tDest\tTime\tPath\n");for (i = 1; i <= m; i++){printf("%d\t%d\t%d", from[i], fire, city[from[i]][fire]);if (from[i] == fire) printf("\t%d\n", fire);else{printf("\t%d", from[i]);PRINT(from[i], fire);printf("\t%d\n", fire);}}}return 0;}
- ZOJ1053-FDNY to the Rescue!(图论之最短路)
- zoj1053/poj1122-FDNY to the Rescue!
- poj1122&zoj1053 FDNY to the Rescue!(dijkstra+输出路径)
- poj 1122||zoj 1053 FDNY to the Rescue!(最短路)
- poj 1122 FDNY to the Rescue! 最短路
- POJ--1122--FDNY to the Rescue!【最短路】
- poj 1122 FDNY to the Rescue! (最短路+打印路径)
- ZPJ - 1053 FDNY to the Rescue!(最短路 Dijkstra),POJ 1122
- POJ 1122 FDNY to the Rescue!(最短路径)
- poj1122 - FDNY to the Rescue!
- 【图论】【最短路】 pku1122 FDNY to the Rescuel
- poj 1122 FDNY to the Rescue! (dijkstra)
- POJ-1122-FDNY to the Rescue!(迪杰斯特拉 反向建图 输出路径)
- Poj 1122 FDNY to the Rescue
- zoj 1053 FDNY to the Rescue!
- POJ:1122 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!
- navicat11激活教程
- 在Ubuntu下利用Eclipse开发FFmpeg配置小结
- 什么是依赖注入
- python爬虫框架-Scrapy安装详细教程
- Pro Git(中文版)
- ZOJ1053-FDNY to the Rescue!(图论之最短路)
- php函数ob_start()、ob_end_clean()、ob_get_contents()
- 在Ubuntu下利用Eclipse调试FFmpeg
- 从零开始学习React Native
- ORACLE 几种同步灾备手段(OGG,ADG,DSG,高级复制,流复制,logmnr)
- Python中的数据标准化
- 洛谷P1047 校门外的树
- javaweb项目只输入ip就可访问无需输入端口号和项目名的实现方法
- react-navigation 如何判断用户是否登录跳转到登录页