POJ 1122.FDNY to the Rescue! Dijkstra
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 2808 | Accepted: 860 |
Description
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
# 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
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 题目链接:http://poj.org/problem?id=1122
题意:有n个交叉路口,N×N的矩阵,edge[i][j]表示第i个交叉路口到底j个交叉路口的时间(注意:edge[i][j]不一定等于edge[j][j])输入一个着火点,接着输入消防站。输出消防站到着火点的时间,具体格式看样例。 思路:终点固定,起点不唯一。把图逆向存储,那么起点就固定了,终点就不唯一。输出最短路径,用Dijkstra算法(这题的输入输出格式需要注意一下) 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 10000000
int n,num,fire;
int edge[][];
int sign[],near[];
int loc[];
struct node
{
int t,p,gg;
} ans[];
int cmp(node a,node b)
{
return a.t<b.t;
}
void Init()
{
cin>>n;
int i,j;
for(i=; i<=n; i++)
{
ans[i].t=INF;
ans[i].gg=;
sign[i]=;
}
for(j=; j<=n; j++)
for(i=; i<=n; i++)
scanf("%d",&edge[i][j]);
scanf("%d",&fire);
}
void Dijkstra(int v)
{
int i,j;
ans[v].t=;
for(i=; i<n; i++)
{
sign[v]=;
int Min=INF,flag;
for(j=; j<=n; j++)
{
if(edge[v][j]>=&&ans[v].t+edge[v][j]<ans[j].t)
{
ans[j].t=ans[v].t+edge[v][j];
near[j]=v;
}
if(sign[j]==&&ans[j].t<Min)
{
Min=ans[j].t;
flag=j;
}
}
v=flag;
}
}
int main()
{
int i,j;
Init();
Dijkstra(fire);
int pp;
while(scanf("%d",&pp)!=EOF)
{
ans[pp].gg=;
}
for(i=; i<=n; i++)
ans[i].p=i;
sort(ans,ans+n+,cmp);
cout<<"Org\tDest\tTime\tPath"<<endl;
for(i=; i<=n; i++)
{
if(ans[i].gg==)
{
printf("%d\t%d\t%d\t",ans[i].p,fire,ans[i].t);
int flag=ans[i].p;
while(flag!=fire)
{
cout<<flag<<"\t";
flag=near[flag];
}
cout<<fire<<endl;
}
}
return ;
}
代码
POJ 1122.FDNY to the Rescue! Dijkstra的更多相关文章
- POJ 1122 FDNY to the Rescue!(最短路+路径输出)
http://poj.org/problem?id=1122 题意:给出地图并且给出终点和多个起点,输出从各个起点到终点的路径和时间. 思路: 因为有多个起点,所以这里反向建图,这样就相当于把终点变成 ...
- POJ 1122 FDNY to the Rescue! Floyd 打印路径就行了
题目大意: 纽约消防部门的支援速度是值得纽约人骄傲的一件事.但是他们想要最快的支援速度,帮助他们提升支援速度他们要调度离着火点最近的一个消防站.他们要你写一个程序来维护纽约消防站的光荣传统.软件需要有 ...
- POJ 1122 FDNY to the Rescue!
给出某些交叉点的距离,-1 表示无法到达. 然后给出火灾发生点 和 附近的消防局位置. 排列消防局 的 时间 与路径. 反向建图,以火灾出发点为起点做一次SPFA. #include<cstd ...
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...
- POJ 1062 昂贵的聘礼(Dijkstra)
题意 : 真真是做POJ第一次遇到中文题,好吧,虽然语言通了,我一开始也没看懂样例什么意思,题意的话就是说这个探险家想娶酋长的女儿,但是没有钱,酋长说他可以用祭司的水晶球或者皮袄来换取少花一部分钱,同 ...
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
随机推荐
- Python - Django - ORM 实例(二)
在 app01/models.py 中添加 Book 类对象表 from django.db import models # Create your models here. # 出版社 class ...
- PyQt5系列教程(八)定时器QTimer的使用
软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 前言 如果需要在程序中周期性地进行某项操作,比如检测某种设备的状态,就会用到定时器.本文就来看看PyQT5中 ...
- python super()函数详解
引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...
- 有关Firefox/Chrome的问题汇总
安装的附加组件因未经验证而被 Firefox 禁用,我该怎么办 如果您已安装的附加组件因未经验证而被禁用了,建议您联系附加组件开发者或提供给您附加组件的人,看看他们能不能提供新版经过验证的附加组件.您 ...
- thinkphp5隐藏apache下的index.php
在应用入口文件同级目录添加.htaccess文件,内容如下: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews Re ...
- forward与redirect的区别
1.从地址栏显示来说forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地址 ...
- JDK1.7之后switch支持string
转自:https://blog.csdn.net/tjcyjd/article/details/9666035 在Java7之前,switch只能支持 byte.short.char.int或者其对应 ...
- mybatis 常用jdbcType数据类型
来自 : http://blog.csdn.net/paincupid/article/details/50922981
- win32允许前置窗口
win32允许前置窗口函数 AllowSetForegroundWindow(HWND hWnd) 该函数允许其他窗口调用SetForegroundWindow()(将窗口设为前置窗口),前提是调用A ...
- python os模块常用命令
python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...