poj1122
FDNY to the Rescue!
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2917 | Accepted: 896 |
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 题目大意:求各个消防站到着火点的最短距离,按照距离短的顺序依次输出该消防点的距离,路径 思路:我们可以把着火点看做源点,消防站看做其他点,然后把题目给的边的距离反向构图,用迪杰斯特拉算法求最短路径 这样求出来的就是各个消防站到着火点的距离了 代码如下:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxs = ;
const int INF = 0x3f3f3f3f;
int N;
int edge[maxs][maxs];
int path[maxs],dist[maxs];
int start,e[maxs];
void dijkstra(int v)
{
bool vis[maxs];
memset(vis,false,sizeof(vis));
for(int i=;i<=N;i++)
{
dist[i]=edge[v][i];
if(edge[v][i]<INF)
path[i]=v;
else
path[i]=-;
}
vis[v]=true;path[start]=;
for(int i=;i<=N;i++)
{
int mins = INF,k=v;
for(int j=;j<=N;j++)
if(!vis[j]&&dist[j]<mins)
{
mins = dist[j];
k=j;
}
vis[k]=true;
for(int j=;j<=N;j++)
if(!vis[j]&&dist[j]>dist[k]+edge[k][j])
{
dist[j]=dist[k]+edge[k][j];
path[j]=k;
}
} } int main()
{
freopen("in.txt","r",stdin);
scanf("%d",&N);
memset(edge,,sizeof(edge));
int d;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
{
scanf("%d",&d);
if(d==-)
edge[j][i]=INF;//反向存储
else
edge[j][i]=d;
}
scanf("%d",&start);
d=;
while(scanf("%d",&e[++d])!=EOF);
printf("Org\tDest\tTime\tPath\n");
dijkstra(start); bool vis[maxs];
memset(vis,false,sizeof(vis));
for(int i=;i<d;i++)
{
int temp = INF,k;
for(int j=;j<d;j++)
if(dist[e[j]]<=temp&&!vis[j])
{
temp=dist[e[j]];
k=j;
}
vis[k]=true;
printf("%d\t%d\t%d\t",e[k],start,dist[e[k]]);
//打印路径
int t = e[k];
if(path[t]==-)
printf("%d\n",e[k]);
else
{
while(path[t]!=)
{
printf("%d\t",t);
t=path[t];
}
printf("%d\n",start);
}
}
return ;
}
poj1122的更多相关文章
- ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)
两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ ...
- poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- 【ACM】那些年,我们挖(WA)过的最短路
不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- 2017年暑假ACM集训日志
20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...
随机推荐
- phython学习
Python 中文学习大本营 关于作者 赞助本站 The Python Tutorial (Python 2.7.X) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教 ...
- apicloud代码压缩和全局加密
首先说代码压缩,因为没什么用,就先说它了.代码压缩后,apicloud里面的css和js文件里面的空格呀回车呀都去掉了,就是文件小了,所有代码显示为一行了.这些代码的变量没有重命名,我们知道jquer ...
- 一次HBase问题的解决过程(Status: INCONSISTENT)
==版本信息== HBase:2.7.1 Storm:1.0.1 RocketMQ:3.4.6(阿里版) ==问题描述== 2018年9月3号晚上23点左右,例行巡检系统运行状况时, 发现Storm消 ...
- 8.14 右键自定义菜单 更加iframe 内容高度调整 iframe高度 js定时
<div class="main_contain" id="z_div" style="position: relative;"> ...
- IIS 6 备忘
用IIS7久了, 回到IIS6 总被搞混,所以记录下,以备忘记. 以下是转载和整合了他人的资源,原出处不详. IIS Web 服务器的权限设置有两个地方,一个是 NTFS 文件系统本身的权限设置, ...
- 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)
传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...
- 44 The shopping psychology 购物心理
The shopping psychology 购物心理 ①People can be addicted to different things ---e. g.,alcohol, drugs, ce ...
- python pip国内源
pip国内的这个源最快 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 修改源方法: 临时使用: 可以在使用pip的时候在后面加上-i参数,指定pip ...
- html转jsp部分css不可用
解决方法 <%String path = request.getContextPath();String basePath = request.getScheme()+"://&quo ...
- BZOJ 1007 [HNOI2008]水平可见直线 (栈)
1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7940 Solved: 3030[Submit][Sta ...