Zoj 1053 (Dijkstra algorithm Shortest Path)

Source: Internet
Author: User

FDNY to the rescue!

Time Limit: 2 seconds
Memory limit: 65536 KB

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
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
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:16 0 3 4-1-1-1-1 0 4 5-1-1 2 3 0-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 24 2 3 4 5 2 6 2 6 5 2

Source:Mid-Atlantic USA2001

Code: C ++

Runtime: 0

Runmemory: 180kb

// Attach my code. My first topic graph theory is to read the algorithm description written by Dijkstra in the shortest path in the algorithm book. // In fact, the overall idea is okay before I start using the map. storage output target, the results can be output repeatedly on the network, so it is changed to queue storage. // then it falls into the cycle-like Wa. The main task is not to calm down and think about it. // In fact, the code is carefully checked again, I found a small mistake and didn't want to be able to solve the problem in the future. This is a necessary condition for qualified acmer! # Include <stdio. h> # include <string. h> # include <ctype. h ># include <queue> using namespace STD; const int maxn = 21; const int INF = 1000000000; struct node {int Ti, X; friendbool operator <(node, node B) {return. ti> B. ti ;}}; priority_queue <node> q; void Dijkstra (int n, int Dist [maxn], int map [maxn] [maxn], int pre [maxn], int s) {int I, j, k; int min; bool P [maxn]; P [s] = true; Pre [s] = 0; for (I = 1; I <= N; I ++) {if (I! = S) {P [I] = false; Dist [I] = map [I] [s]; Pre [I] = s ;}} for (I = 1; I <= n-1; I ++) {min = inf; k = 0; For (j = 1; j <= N; j ++) {If (! P [J] & Dist [J]! = Inf & Dist [J] <min) {min = DIST [J]; k = J ;}} if (k = 0) return; P [k] = true; For (j = 1; j <= N; j ++) {If (! P [J] & map [J] [k]! = Inf & Dist [k] + map [J] [k] <Dist [J]) {Dist [J] = DIST [k] + map [J] [k]; Pre [J] = K ;}}} void output (int x, int pre []) {If (X! = 0) {printf ("\ t % d", x); output (pre [X], pre) ;}} int main () {int Dist [maxn], map [maxn] [maxn], pre [maxn]; int t; int N; int num; int s; char X [10000]; int A [maxn]; node P; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d", & map [I] [J]); if (Map [I] [J] =-1) map [I] [J] = inf;} scanf ("% d", & S); num = 0; getchar (); gets (x); int Len = strlen (x); int TMP = 0; For (INT I = 0; I <= Len; I ++) {If (isdigit (X [I]) TMP = TMP * 10 + X [I]-'0'; else if (TMP) {A [num ++] = TMP; TMP = 0 ;}} memset (PRE, 0, sizeof (pre); memset (Dist, 0, sizeof (DIST )); dijkstra (n, DIST, MAP, pre, S); For (INT I = 0; I <num; I ++) {If (Dist [A [I]! = Inf) {P. ti = DIST [A [I]; p. X = A [I]; q. push (p) ;}} printf ("org \ tdest \ tTime \ tpath \ n"); While (! Q. empty () {node front = Q. top (); printf ("% d \ t % d", front. x, S, front. ti, front. x); output (pre [front. x], pre); printf ("\ n"); q. pop () ;}if (T! = 0) printf ("\ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.