FDNY to the Rescue!
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
Source
题目类型:最短路
算法分析:直接使用Dij计算即可,注意要将计算出来的多组数据按照时间排序输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100 + 66; struct node { int org, dest, time, pre[maxn], prelen; }; node out[maxn]; int len; bool vis[maxn];//表示节点的最短路是否已经得到 //分别表示邻接矩阵、节点最短路的长度和顶点的个数 int edge[maxn][maxn], dis[maxn], n, path[maxn]; void Dijkstra (int s) { memset (vis, false, sizeof (vis));; memset (path, -1, sizeof (path)); vis[s] = true; for (int i = 1; i <= n; i++)//初始化源点相邻接的节点的最短距离 { dis[i] = edge[s][i]; if (edge[s][i] < INF) path[i] = s; } dis[s] = 0;//源点到自己的最短路是0,十分重要!!! path[s] = s; for (int i = 2; i <= n; i++) { int minval = INF, minpos = s; for (int j = 1; j <= n; j++)//遍历将还未确定最短路的节点中具有最小距离的的节点找到 if (j != s && !vis[j] && minval > dis[j]) { minval = dis[j]; minpos = j; } vis[minpos] = true;//访问该节点 for (int j = 1; j <= n; j++)//更新由于minpos节点确定最短路之后导致的最小距离变化 if (j != s && !vis[j] && edge[minpos][j] < INF && edge[minpos][j] + dis[minpos] < dis[j]) { dis[j] = edge[minpos][j] + dis[minpos]; path[j] = minpos; } } } bool cmp (node aa, node bb) { return aa.time < bb.time; } int main() { // ifstream cin ("aaa.txt"); while (cin >> n) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { cin >> edge[i][j]; if (edge[i][j] == -1) edge[i][j] = INF; } string s; getline (cin, s); getline (cin, s); stringstream ss (s); int aa, bb; ss >> aa; len = 0; while (ss >> bb) { Dijkstra (bb); out[len].dest = aa; out[len].org = bb; out[len].time = dis[aa]; // cout << dis[aa] << endl; out[len].prelen = 0; int p = aa; while (p != bb) { out[len].pre[out[len].prelen] = p; out[len].prelen++; p = path[p]; } out[len].pre[out[len].prelen] = bb; out[len].prelen++, len++; } sort (out, out + len, cmp); cout << "Org" << '\t' << "Dest" << '\t' << "Time" << '\t' << "Path" << endl; for (int i = 0; i < len; i++) { cout << out[i].org << '\t' << out[i].dest << '\t' << out[i].time; for (int j = out[i].prelen - 1; j >= 0; j--) cout << '\t' << out[i].pre[j]; cout << endl; } } return 0; } |
- « 上一篇:poj1088
- poj1125:下一篇 »