Poj1122_fdny to the rescue! (Reverse graph creation + Shortest Path Tree)

Source: Internet
Author: User
FDNY to the rescue!
Time limit:1000 ms   Memory limit:10000 K
Total submissions:2368   Accepted:721

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

OrgDestTimePath52252423452626652

Source

Mid-Atlantic 2001 Problem Solving report afternoon re afternoon, inexplicable upset... (Is it because the afternoon simulated Invitational competition is suspended ...) Try again in the dormitory at night, AC... Although the graph is an undirected graph, the time from I to J is not the time from J to I. The fire brigade must start from the fire station to reach the fire source, the fire source is fixed, so reverse graph creation is the best solution. Find the shortest time to arrive at the fire station from the fire source. Second, we need to print the path for this question. We seldom encounter this kind of question. In fact, we use arrays to process the relationship between trees. The array stores the parent node of the current serial number node, which can be searched at a time, is to traverse the path tree... The shortest path is the bare shortest path...
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define inf 99999999#define Max 30using namespace std;int n,mmap[Max][Max],dis[Max],vis[Max],pre[Max];struct node{    int org,dest,time;} num[Max];int cmp(node a,node b){    return a.time<b.time;}void dij(int s){    int i,j,u,minn;    for(i=1; i<=n; i++)    {        dis[i]=mmap[s][i];        vis[i]=0;        if(i!=s)            pre[i]=s;        else pre[i]=-1;    }    dis[s]=0;    vis[s]=1;    for(i=0; i<n-1; i++)    {        u=0;        minn=inf;        for(j=1; j<=n; j++)        {            if(!vis[j]&&dis[j]<minn)            {                minn=dis[j];                u=j;            }        }        vis[u]=1;        for(j=1; j<=n; j++)        {            if(!vis[j]&&dis[j]>dis[u]+mmap[u][j])            {                dis[j]=dis[u]+mmap[u][j];                pre[j]=u;            }        }    }}int main(){    int i, j, s, u, cnt=0;    cin>>n;    for(i=1; i<=n; i++)    {        for(j=1; j<=n; j++)        {            scanf("%d",&mmap[j][i]);            if(mmap[j][i]==-1)                mmap[j][i]=inf;        }    }    cin>>s;    dij(s);    while(cin>>u)    {        num[cnt].org=u;        num[cnt].dest=s;        num[cnt++].time=dis[u];    }    printf("Org\tDest\tTime\tPath\n");    int shortest[Max];    sort(num,num+cnt,cmp);    for(i=0; i<cnt; i++)    {        printf("%d\t%d\t%d",num[i].org,num[i].dest,num[i].time);        memset(shortest,0,sizeof(shortest));        int k=0;        shortest[k]=num[i].org;        while(pre[shortest[k]]!=-1)        {            k++;            shortest[k]=pre[shortest[k-1]];        }//        k++;//        shortest[k]=s;        for(j=0;j<=k;j++)        printf("\t%d",shortest[j]);        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.