POJ 1122.FDNY to the rescue!

Source: Internet
Author: User

FDNY to the rescue!
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2808 Accepted: 860

Description

The fire Department of New York (FDNY) have always been proud of their response time to fires in New York City, but they WA NT 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 Addre SS in the city. You have been hired to write this software and is entrusted with maintaining the proud tradition of FDNY. Conceptually, the software'll be given the address of the fire, the locations of the firehouses, street intersections, a nd the time it takes to cover the distance between each intersection. It would then use the this information to calculate what long it takes to reach a address from each firehouse.

Given a specific fire at the city, the software would calculate the time taken from all the fire stations located The fire location of the city to reach. The list of fire stations is 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 E Very pair of intersections in the city. The sample input shown below the value "3" on the 1st row and the 2nd column represents the time taken from Intersectio n #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 Intersec tion #2.

A value of-1 for time means that it's not possible to go directly from the origin intersection (row #) to the Destinatio n Intersection (column #). All and the values in the table is non-negative.

Line n+2:
An integer value n (<= N) indicating the intersection closest to the fire location followed by one or more integer valu Es for the intersections closest to the fire stations (all on one line, separated by one or more spaces) would follow the I Nput Matrix.

Notes on input format:

1. The rows and columns is numbered from 1 to N.
2. All input values is integers
3. All fire locations is guaranteed reachable from all firehouses.
4. All distance calculations is made from the intersection closest to each firehouse to the intersection closest to the F Ire.

Output

Line 1:
A label line with the headings for each column, exactly as shown in the example.

Line 2 onwards (one line to 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 is tab separated.
2. If or more firehouses is tied in time they can is printed in any order.
3. If more than one path exists that have the same minimal time for a given location & firehouse, either one can be PRI Nted on the output.
4. If the fire location and the fire station locations happen to be the same intersection, the output would indicate that T He origin and destination have the same intersection number, the time would be ' 0 ' and the nodes in the shortest path would 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  

Sample Output

OrgDestTimePath52252423452626652

Title Link: http://poj.org/problem?id=1122
Test instructions: There are N intersections, nxn matrices, Edge [I][j] indicates the time at which intersection of the first I intersection is J (Note: Edge[i][j] does not necessarily equal edge[j][j]) Enter a fire point and enter the fire station. Output fire station to the ignition point of time, the specific format to see sample.

Idea: The end is fixed, the starting point is not unique. The image is stored in reverse, then the starting point is fixed and the end point is not unique. Output shortest path, with Dijkstra algorithm (this problem input and output format need to pay attention to)

Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineINF 10000000intN,num,fire;intedge[ -][ -];intsign[ -],near[ -];intloc[ -];structnode{intT,p,gg;} ans[ -];intCMP (node A,node b) {returna.t<b.t;}voidInit () {cin>>N; inti,j;  for(i=1; i<=n; i++) {ans[i].t=INF; Ans[i].gg=0; Sign[i]=0; }     for(j=1; j<=n; J + +)         for(i=1; i<=n; i++) scanf ("%d",&Edge[i][j]); scanf ("%d",&Fire );}voidDijkstra (intv) {    inti,j; ANS[V].T=0;  for(i=0; i<n; i++) {Sign[v]=1; intmin=Inf,flag;  for(j=1; j<=n; J + +)        {            if(edge[v][j]>=0&&ans[v].t+edge[v][j]<ans[j].t) {ans[j].t=ans[v].t+Edge[v][j]; NEAR[J]=v; }            if(sign[j]==0&&ans[j].t<Min) {Min=ans[j].t; Flag=J; }} v=Flag; }}intMain () {inti,j;    Init ();    Dijkstra (fire); intpp;  while(SCANF ("%d", &pp)! =EOF) {Ans[pp].gg=1; }     for(i=1; i<=n; i++) ANS[I].P=i; Sort (Ans,ans+n+1, CMP); cout<<"Org\tdest\ttime\tpath"<<Endl;  for(i=1; i<=n; i++)    {        if(ans[i].gg==1) {printf ("%d\t%d\t%d\t", ans[i].p,fire,ans[i].t); intflag=ANS[I].P;  while(flag!=Fire ) {cout<<flag<<"\ t"; Flag=Near[flag]; } cout<<fire<<Endl; }    }    return 0;}
Code

POJ 1122.FDNY to the rescue!

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.