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

Mid-Atlantic 2001

题意:求各个消防点到失火点的距离和到达该点的路径。

思路:dijkstra求最短路,因为失火点只有一个,故可以求失火点到消防局的最短路,读入反向边。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 25
#define inf 0x7fffffff
int mark[N],g[N][N];
int dis[N],pre[N],n;  //记录最短路,记录路线
struct node
{
    int id,d;  //记录消防局小标和距离
}a[N];
bool cmp(node a,node b)
{
    return a.d<b.d;
}
void dijkstra(int s)
{
    int i,min,u;
	memset(pre,-1,sizeof(pre));
    for(i=1;i<=n;i++)
    {
        pre[i]=s;
        dis[i]=g[s][i];
        mark[i]=0;
    }
    mark[s]=1;
    while(1)
    {
        min=inf;
        u=s;
        for(i=1;i<=n;i++)
        {
            if(min>dis[i]&&!mark[i])
            {
                min=dis[i];
                u=i;
            }
        }
        if(u==s)
            break;
        mark[u]=1;
        for(i=1;i<=n;i++)
        {
            if(!mark[i]&&g[u][i]<inf&&g[u][i]+dis[u]<dis[i])
            {
                dis[i]=g[u][i]+dis[u];
                pre[i]=u;
            }
        }
    }
}
void work(int k)
{
    int i,j;
    a[0].d=-1;
    for(i=1;i<k;i++)
    {
        a[i].d=dis[a[i].id];
    }
    sort(a,a+k,cmp);
    printf("Org	Dest	Time	Path\n");
    for(i=1;i<k;i++)
    {
        printf("%d	%d	%d	%d	",a[i].id,a[0].id,a[i].d,a[i].id);
        for(j=pre[a[i].id];j!=a[0].id;j=pre[j])
        {
            printf("%d	",j);
        }
		if(a[i].id!=a[0].id)      //失火点和消防点相同
	        printf("%d\n",a[0].id);
		else
			printf("\n");
    }
}
int main()
{
    int i,j,t,k;
    char str[500];
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&g[j][i]);
                if(g[j][i]<0)
                    g[j][i]=inf;
            }
        }
        gets(str);
        gets(str);
        t=k=0;
        for(i=0;str[i]!='\0';i++)
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                t=t*10+str[i]-'0';
            }
            else if(t!=0)
            {
                a[k++].id=t;
                t=0;
            }
        }
		if(t!=0)      //末尾还有一个
			a[k++].id=t;
        memset(pre,-1,sizeof(pre));
        dijkstra(a[0].id);
        work(k);
    }
    return 0;
}

poj 1122 FDNY to the Rescue! (dijkstra)

时间: 2024-10-13 08:42:23

poj 1122 FDNY to the Rescue! (dijkstra)的相关文章

POJ 1122 FDNY to the Rescue! 反向dijkstra

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

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

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

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

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

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

poj1062昂贵的聘礼(Dijkstra**)

1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u>0, i>0) 4 g[u][0]表示不用替换该物品的实际价格 ! 5 d[0]表示的是第一个物品经过一系列的物品替换之后的最少优惠价格! 6 7 思路:每当我们通过Dijkstra算法得到离源点(1)最近的距离的节点 p的时候(也就是1...pre[p], p)这条 8 路径上的物品互相替换后得

poj 3903 &amp; poj 2533 最长上升子序列(LIS)

最长上升子序列. 做这道题之前先做了2533,再看这道题,感觉两道题就一模一样,于是用2533的代码直接交, TLE了: 回头一看,数据范围.2533 N:0~1000:3903 N :1~100000. 原因终归于算法时间复杂度. 也借这道题学习了nlgn的最长上升子序列.(学习链接:http://blog.csdn.net/dangwenliang/article/details/5728363) 下面简单介绍n^2 和 nlgn 的两种算法. n^2: 主要思想:DP: 假设A1,A2..

迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中.在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v