题目链接:

http://poj.org/problem?id=2607

Description

A city is served by a number of fire stations. Some residents have complained that the distance from their houses to the nearest station is too far, so a new station is to be built. You are to choose the location of the fire station so as to reduce the distance to the nearest station from the houses of the disgruntled residents.
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

The
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

You
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打表+枚举更新最优)的更多相关文章

  1. POJ 2607 Fire Station

    Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  2. Nyoj Fire Station

    描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...

  3. POJ 2607

    一次FLOYD,再枚举. 注意题目要求的输出是什么哦. #include <iostream> #include <cstdio> #include <cstring&g ...

  4. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  5. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  6. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  7. Oracle 两个表之间更新的实现

    Oracle 两个表之间更新的实现   来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...

  8. sqlserver多表连接更新

    一.MS SQL Server 多表关联更新 sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来.虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据 ...

  9. Postgresql两表联结更新

    Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name ,    descrip ...

随机推荐

  1. hive中数据存储格式对比:textfile,parquent,orc,thrift,avro,protubuf

    这篇文章我会从业务中关注的: 1. 存储大小 2.查询效率 3.是否支持表结构变更既数据版本变迁 5.能否避免分隔符问题 6.优势和劣势总结 几方面完整的介绍下hive中数据以下几种数据格式:text ...

  2. 关于CentOS下 yum包下载下的rpm包放置路径

    在CentOS下用yum安装,回发现在/var/cache/yum/下的base.extrs和updates下的packages下都没有发现下载的RPM 原来在/etc/yum.conf下没有设置下载 ...

  3. Windows10卡顿,磁盘 内存占用100%或比较多

    1.关闭服务:Superfetch: 2.结束antimalware service executable进程,gpedit.msc下依次点击“计算机配置/管理模板/Windows组件/Windows ...

  4. 经过实际验证的C#调用Haskell的方法

    [系统环境] Windows Server 2008 R2,Haskell Platform 2013.2.0.0,ghc 7.6.3,cabal 1.16.0 [操作步骤] 1. 安装Windows ...

  5. Python 数据结构与算法—— 快排

    1. 先从待排序的数组中找出一个数作为基准数(取第一个数即可),然后将原来的数组划分成两部分:小于基准数的左子数组和大于等于基准数的右子数组.然后对这两个子数组再递归重复上述过程,直到两个子数组的所有 ...

  6. VS2017 v15.8.0 Task ExpandPriContent failed. Illegal characters in path

    昨天更新了VS到最新版本v15.8.0,但是编译UWP出现了操蛋的bug. 谷歌一下,vs社区已经有答案了. 打开.csproj文件,在节点 <PropertyGroup> 里面,加上一行 ...

  7. MariaDB 数据库系统概述(1)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  8. es6新增Math方法

    Math.trunc()  用于去除一个数的小数部分,只返回整数部分 Math.trunc(4.1) // 4 Math.trunc(4.9) // 4 Math.trunc(-4.1) // -4 ...

  9. Java并发编程总结2——慎用CAS

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...

  10. C、C++打包成.dll .so .a 给Unity使用

    C.C++打包成.dll .so .a 给Unity使用 打包.dll库 工具:VS 使用VS新建项目 选择不大于.NET3.5的版本 选择Visual C++ -> Win32 控制台应用程序 ...