POJ 2607 Fire Station(Floyd打表+枚举更新最优)
题目链接:
http://poj.org/problem?id=2607
Description
The city has up to 500 intersections, connected by road segments of
various lengths. No more than 20 road segments intersect at a given
intersection. The location of houses and firestations alike are
considered to be at intersections (the travel distance from the
intersection to the actual building can be discounted). Furthermore, we
assume that there is at least one house associated with every
intersection. There may be more than one firestation per intersection.
Input
first line of input contains two positive integers: f,the number of
existing fire stations (f <= 100) and i, the number of intersections
(i <= 500). The intersections are numbered from 1 to i consecutively.
f lines follow; each contains the intersection number at which an
existing fire station is found. A number of lines follow, each
containing three positive integers: the number of an intersection, the
number of a different intersection, and the length of the road segment
connecting the intersections. All road segments are two-way (at least as
far as fire engines are concerned), and there will exist a route
between any pair of intersections.
Output
are to output a single integer: the lowest intersection number at which
a new fire station should be built so as to minimize the maximum
distance from any intersection to the nearest fire station.
Sample Input
1 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
Sample Output
5
Source
/*
问题
给出已经是消防站的个数和每个消防站在哪个顶点上,给出以EOF结尾的边
计算并输出新建一个消防站,使得各个站点距离自己最近的消防站的距离最小,比如样例中选择新建在5号,使得各个顶点距离自己最近的
消防站的距离最远是10,而选择在其他点时有的站点距离自己最近的消防站比10大,所以选择5号顶点,另外就是可以选择已经有消防站的
点新建,因为题目要有新建的顶点的标号尽量小。 解题思路
500个站点使用Floyd算出各个顶点到各个顶点的最短距离,方便之后枚举新站点时直接查表得到两点的最短距离,然后依次枚举,找出各个
站点距离自己最近的消防站的最大距离,更新最优,最后输出标号即可。
*/
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=;
const int INF=;
int e[maxn][maxn],book[maxn],ans[maxn],m; void Floyd(); int main()
{
int f,t1,i,j,t2,t3,t4;
while(scanf("%d%d",&f,&m) != EOF){
memset(book,,sizeof(book));
for(i=;i<=m;i++)
ans[i]=INF; for(i=;i<f;i++){
scanf("%d",&t1);
book[t1]=;//标记是消防站
} for(i=;i<=m;i++){
for(j=;j<=m;j++){
e[i][j] = i==j?:INF;
}
} while(scanf("%d%d%d",&t2,&t3,&t4) != EOF){
e[t2][t3]=e[t3][t2]=min(t4,e[t2][t3]);
}
Floyd(); //ans数组表示各个站点距离离自己最近的消防站的距离
for(i=;i<=m;i++){
if(book[i])
ans[i]=;
else
for(j=;j<=m;j++){
if(book[j])
ans[i]=min(ans[i],e[i][j]);
}
}
/*for(i=1;i<=m;i++){
printf("%d ",ans[i]);
}
printf("\n");*/ int ansmin=INF;
int biaohao;
for(i=;i<=m;i++){//枚举每个站点是消防站,找到该点是新建消防站时距离该点最远的点并记录距离,如果该距离是最小的
//记录该点最后输出
int temp=-;
for(j=;j<=m;j++)
temp = max(min(e[i][j],ans[j]),temp); if(ansmin > temp){
ansmin = temp;
biaohao=i;
}
} printf("%d\n",biaohao);
}
return ;
} void Floyd()
{
int i,j,k;
for(k=;k<=m;k++)
for(i=;i<=m;i++)
for(j=;j<=m;j++)
if(e[i][j] > e[i][k]+e[k][j])
e[i][j] = e[i][k]+e[k][j];
}
POJ 2607 Fire Station(Floyd打表+枚举更新最优)的更多相关文章
- POJ 2607 Fire Station
Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- Nyoj Fire Station
描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...
- POJ 2607
一次FLOYD,再枚举. 注意题目要求的输出是什么哦. #include <iostream> #include <cstdio> #include <cstring&g ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
题意: 给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- Oracle 两个表之间更新的实现
Oracle 两个表之间更新的实现 来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...
- sqlserver多表连接更新
一.MS SQL Server 多表关联更新 sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来.虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据 ...
- Postgresql两表联结更新
Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name , descrip ...
随机推荐
- AngularJS $scope 继承性 作用 生命周期
一.基本概念 作用域是一个指向应用模型的对象,相当于MVVM中的ViewModel,能绑定数据(属性)和行为(方法),能监控表达式和传递事件,是实现双向绑定的基础,是应用在 HTML (视图) 和 J ...
- pycharm的基本使用
1.设置注释字体的颜色,如下图 2.常用快捷键 Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 类名完成 Ctrl + Shift + Enter 语 ...
- Android-Java-等待唤醒机制原理
儿时的游戏:(等待 与 唤醒) 有一群小朋友一起玩一个游戏,这个游戏可能大家都玩过,大家一起划拳,划拳输得最惨的那个小朋友去抓人(这个小朋友取名为 CPU),被抓的很多人取名为线程,有很多线程,如果其 ...
- WPF 定义Lookless控件的默认样式、 OnApplyTemplate 如何使用(实现方式、如何工作的)!
写的非常详细: 作者地址:https://www.cnblogs.com/atskyline/archive/2012/11/16/2773806.html 参考资料: http://www.code ...
- WPF圆角按钮例程
<Window x:Class="WpfApp3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...
- BigDecimalUtils
package com.sprucetec.tms.utils; import java.math.BigDecimal;import java.text.SimpleDateFormat;impor ...
- wcf返回值报错解析
问题来源 最近在项目中使用wcf,因为是一个新手,对新的东西总是比较敬畏,不过一切都是进行得很顺利,运行的时候,突然报了错,编译器提示的错误大概是:“InvalidOperationException ...
- postgresql-pgbench(转)
pgbench测试: pg9.6.2的pgbench报错: [thunisoft@localhost ~]$ pgbench -S -c 8 -t 60 pgbenchdb Segmentatio ...
- linux安装mysql数据库和简单配置
- 关于如何食用Xcode——用mac的小蒟蒻
前言QwQ 对于一只用Mac的小蒟蒻,没有Dev_c++简直太难受了,用在线IDE写代码又没法保存,那么我们怎么办呢? 好在App Store里有这个好东西 所以我们今天来介绍一下 “如何使用Xcod ...