ZOJ 1053 FDNY to the Rescue!(最短路)

题目链接

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 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

题意:给一个无向图,1个终点,若干起点,让你求若干起点到终点的最短路和最短路径。

AC代码


#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;
const int N = 30;
const int inf = 0x3f3f3f3f;
struct node{
    int u, v, w;
    int path[N];
}q[N*N];
int n, ed, cnt;
int e[N][N], dis[N], P[N];
bool vis[N];
char s[1000];
void Dij(int st){
    memset(vis, false, sizeof(vis));
    vis[st] = true;
    for(int i = 1; i<= n; i++){
        dis[i] = e[st][i];
        q[cnt].path[i] = st;
    }
    q[cnt].path[st] = 0;
    for(int  i = 1; i < n; i++){
        int minn = inf, u;
        for(int j = 1; j <= n; j++){
            if(dis[j] < minn && !vis[j]){
                minn = dis[j];
                u = j;
            }
        }
        if(minn == inf) break;
        vis[u] = true;
        for(int j = 1; j <= n; j++){
            if(dis[j] > dis[u] + e[u][j] && !vis[j]){
                dis[j] = dis[u] + e[u][j];
                q[cnt].path[j] = u;
            }
        }
    }
}
bool cmp(node a, node b){
    return a.w < b.w;
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        cnt = 0;
        for(int i = 1; i <= n; i++){
            for(int  j = 1; j <= n; j++){
                scanf("%d", &e[i][j]);
                if(e[i][j] == -1) e[i][j] = inf;
            }
        }
        scanf("%d", &ed);
        gets(s);
        int i = 0;

        int len = strlen(s);
        while(i < len){
            int st = 0;
            while(s[i] < '0' || s[i] > '9'){
                i++;
            }
            while(s[i] >= '0' && s[i]<='9'){
                st = st * 10 + s[i] - '0';
                i++;
            }
            Dij(st);
            q[cnt].u = st;
            q[cnt].v = ed;
            q[cnt++].w = dis[ed];
            i++;
        }
        sort(q, q + cnt, cmp);
        printf("Org\tDest\tTime\tPath\n");
        for(int i = 0; i < cnt; i++){
            printf("%d\t%d\t%d\t", q[i].u, q[i].v, q[i].w);
            stack<int> S;
            int ss = q[i].v;
            while(ss){
                S.push(ss);
                ss = q[i].path[ss];
            }
            bool flag = false;
            while(!S.empty()){
                if(!flag) flag = true;
                else printf("\t");
                printf("%d", S.top());S.pop();
            }
            printf("\n");
        }
        if(t != 0) printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kun-/p/9844794.html

时间: 2024-10-10 12:21:48

ZOJ 1053 FDNY to the Rescue!(最短路)的相关文章

POJ 1122 FDNY to the Rescue!(最短路)

题目链接 FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3405 Accepted: 1063 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 t

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

poj 1122 FDNY to the Rescue! (dijkstra)

FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2475   Accepted: 755 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 the

POJ 1122.FDNY to the Rescue!

FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2808   Accepted: 860 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 the

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

POJ 1122 FDNY to the Rescue! Floyd 打印路径就行了

题目大意: 纽约消防部门的支援速度是值得纽约人骄傲的一件事.但是他们想要最快的支援速度,帮助他们提升支援速度他们要调度离着火点最近的一个消防站.他们要你写一个程序来维护纽约消防站的光荣传统.软件需要有的功能是,能获取着火点的地址 和 消防站的位置, 街道交叉路口, 从一个交叉路口到达另一个交叉路口的时间. 他将要计算从消防站到达着火点需要多少时间. 给你一个具体的着火点的地址,这个软件应该找出所有消防站到达着火点的距离, 并且从小到大进行排序.以便消防员来调度人员到达救火地点. #include

POJ 1122 FDNY to the Rescue! 反向dijkstra

链接: 1122 题意: 一个城市中有N个交叉路口,给出从一个交叉路口i到另一个交叉路口j所需要的时间(i,j=1~N,单向)如果edge[i][j]=-1 则表示不通 给出一个火警的位置(终点) 和X个消防站(起点) 输出:每一行描述了一个消防站的信息,这些信息按消防站到达火警位置所需时间从小到大排列.这些信息包括:消防站的位置(初始位置).火警位置(目标位置).所需时间以及最短路径上的每个交叉路口. 题解: 起点有多个,终点只有一个.为了只进行一次dijkstra算法 我们可以考虑从终点出发

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive