题目链接:

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. Spring中注入bean学习的总结

    1.在类上直接加注解@Component,那么这个类就直接注入到Spring容器中了  ,像@Contrloller,@Service这些本质上都是@Component, 2.@Configurati ...

  2. 分频器的verilog设计

    笔者最近由于实验室老师的任务安排重新又看了一下分频器的verilog实现,现总结如下,待以后查看之用(重点是查看计数器计到哪个值clk_out进行状态翻转) 1.偶数分频占空比为50% 其实质还是一个 ...

  3. FNDLOAD使用大全

    FNDLOAD使用大全   Syntax FNDLOAD [username/password] 0 Y [mode] [configuration file] [target data file] ...

  4. 分形之二叉树(Binary Tree)

    上一篇文章讲的是分形之树(Tree),这一篇中将其简化一下,来展示二叉分形树的生长过程. 核心代码: static void FractalBinaryTree(const Vector3& ...

  5. TaskCreationOptions.LongRunning 运行比可用线程数更多的任务

    最近在学WebSocket,服务端需要监听多个WebSocket客户端发送的消息. 开始的解决方法是每个WebSocket客户端都添加一个线程进行监听,代码如下: /// <summary> ...

  6. ServiceStack DateTime数据类型转Json出现的困扰

    执行dotnet-new selfhost sstest 创建项目,然后打开解决方案 修改ssTest.ServiceModel中的Hello.cs,在HellopResponse中添加时间属性,然后 ...

  7. WPF 内存泄漏优化经历

    最近公司有个CS客户端程序,有个登录界面,有个程序的主界面,程序支持注销功能,但是在注销后,客户端的内存一直以40M-50M的速度递增,因此猜测,应该是WPF程序出现了内存泄漏.下面主要记录优化内存泄 ...

  8. css绘制特殊图形,meida查询,display inline-box间隙问题以及calc()函数

    本文同时发表于本人个人网站 www.yaoxiaowen.com 距离上一篇文章已经一个月了,相比于写代码,发现写文章的确是更需要坚持的事情.言归正传,梳理一下这一个月来,在写ife任务时,有必要记录 ...

  9. Redis---事务和Wtach

    1. 概述 Redis通过 MULTI, EXEC / WATCH 等命令来实现事务. 事务提供一种将多个命令请求打包, 然后一次性.按顺序的执行多个命令的机制. 并且在事务执行期间, 服务器不会中断 ...

  10. Centos 7 开启BBR

    # 升级内核 rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elr ...