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, ...
随机推荐
- [Selenium]对弹出的Alert窗口进行操作
Alert alert = driver.switchTo().alert(); alert.accept();
- filter 死循环(tomcat 启动完成 ,自动执行filter.dofilter,导致tomcat 启动超时) , tomcat 启动和 servers 启动 不同
package com.diancai.interceptor; import java.io.IOException; import javax.servlet.Filter; import jav ...
- 【Linux】MySQL配置
安装环境/工具 Linux( centOS 版) MySQL(MySQL-5.6.28-1.el7.x86_64.rpm-bundle.tar版) MySQL的目录结构 安装已经说过了,这里不再说了 ...
- pthread_once 和 pthread_key
http://blog.csdn.net/rickyguo/article/details/6259410 一次性初始化 有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到). ...
- 2018.10.19 bzoj1584: Cleaning Up 打扫卫生(线性dp)
传送门 dp妙题. 考虑到每个位置分一组才花费nnn的贡献. 因此某一段不同的数的个数不能超过sqrt(n)sqrt(n)sqrt(n),于是对于当前的位置iii我们记pos[j]pos[j]pos[ ...
- 2018.10.08 NOIP模拟 栅栏(树状数组+rand)
传送门 今天的送分题. 首先考虑每次给要围上栅栏的矩阵里的整体加上1,如果栅栏被撤销就整体减1,最后比较两个点的值是否相同来进行判断. 然而这样的效果并不理想,很容易卡掉. 进一步思考,我们第iii次 ...
- 2018.09.15 hdu1599find the mincost route(floyd求最小环)
传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...
- C# winIO32位,64位的使用(运行时要用管理员身份)
下载地址: http://www.internals.com/utilities/WinIo.zip 一个按键的消息产生流程如下: 1)硬件中断/硬件端口数据 WinIO能模拟,或者修改IDT是在这一 ...
- centos6 mysql 安装与配置
MySQL简介: 由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库.MySQL是一个多用户.多线程的关系型数据库管理 ...
- 2) 下载Maven
http://maven.apache.org/ http://maven.apache.org/download.cgi Maven 3.3.3 (Binary tar.gz) Maven 3.3. ...