题目链接:

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. SQL语句 删除表user 中字段name 内容重复的记录,

    public class T01 { public static void main(String[] args) { int j=4; j=j+=j-=j*=j; System.out.printl ...

  2. MacOS卸载Jenkins安装包

    /Library/Application\ Support/Jenkins/Uninstall.command

  3. ASP.NET WebAPI (反)序列化用[SerializableAttribute]修饰的类的一个坑

    发现问题 在 ASP.NET WebAPI 项目中,有这样的 ViewModel 类: [Serializable] class Product { public int Id { get; set; ...

  4. Linux下安装MySQL以及一些小坑

    第一次写博客,各位凑合着看吧(假装有人看). 我这里使用的是centos7. 1.首先打开终端,查看有没有安装过MySQL: [root@localhost lyp]# rpm -qa | grep ...

  5. docker容器间跨主机通信

    http://jnzg905.iteye.com/blog/2269583 https://blog.csdn.net/pingpangbing0902/article/details/7823889 ...

  6. diamond的设计思路

    diamond主要包含四个包:diamond-client.diamond-sdk.diamond-server和diamond-util client就非常简单的进行http的调用server拿数据 ...

  7. 转---如何让前端更安全?——XSS攻击和防御详解

    前言 平时很少关注安全这块的技术,曾经也买过一本<Web前端黑客技术揭秘>但至今还没翻过,尴尬.今天的早读文章由腾讯优测@小吉带来的分享. 正文从这开始~ 最近深入了解了一下XSS攻击.以 ...

  8. yarn 学习 小记

    官网:https://yarnpkg.com/zh-Hans/docs/installing-dependencies 简介:包管理工具,和npm类似主要特点:快速.安全.可靠 快速:本地安装包后,会 ...

  9. pringboot+mybatis+redis+cookie单点登录

    一.基本思路 单点sso用于多系统分布式,当多个系统分布式部署后,当然需要统一的登录接口.sso应运而生. 可以想见,单点应该是提供一个服务给其他系统,当其他系统需要验证登录状态的时候,调用服务,就可 ...

  10. main函数中如何等待协程运行完毕

    使用channel同步 package main import ( "fmt" ) func printNumber(num int, c chan struct{}) { fmt ...