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 ...
随机推荐
- fastscript例子一
fastscript例子一 fastscript例子一 unit Unit1; interface usesWinapi.Windows, Winapi.Messages, System.SysU ...
- Android-Kotlin-Activity直接的跳转
1.选中应用包名packageName,右键: 2.选中Kotlin: 3.创建Kotlin的Activity完成: 第一个Activity,MainActivity package cn.kotli ...
- Android-Java单例模式
今天我们来说说一个非常常用的模式,单例模式,单例模式让某个类中有自己的实例,而且只实例化一次,避免重复实例化,单例模式让某个类提供了全局唯一访问点,如果某个类被其他对象频繁使用,就可以考虑单例模式,以 ...
- Network in Network 个人理解
关键点是1*1的卷积是怎么用的,也就是MLP卷积.看如下的数据就可以理解了 输入为224*224*3的图像,第一层卷积为11*11*3*96(也就是96个11*11*3的卷积核,另外还有步长),卷积完 ...
- 使用PerfView监测.NET程序性能(二):Perfview的使用
在上一篇博客中,我们了解了对Windows及应用程序进行性能分析的基础:Event Trace for Windows (ETW).现在来看看基于ETW的性能分析工具——Perfview.exe Pe ...
- redhat安装docker
一.禁用selinux 由于Selinux和LXC有冲突,所以需要禁用selinux.编辑/etc/selinux/config,设置两个关键变量. SELINUX=disabled SEL ...
- ASP.NET MVC使用SignalR统计在线用户人数
学到新东西就记录一下.也许正好有人需要~~~~~~ 由于需要记录当前在线用户,emmmm又是没做过的... 本来想用数据库的形式,但是想想这么简单的功能百度肯定有.遨游一波百度,有所收获.... 虽然 ...
- Oracle.ManagedDataAccess.Client.OracleException:“ORA-00936: 缺失表达式”
static void Main(string[] args) { string sql = "insert into StudentC(Stuid, Stuname, Stupass) v ...
- DataFrame 操作
# 删除td数据框中的 指定列 td.drop(columns=['ship-city'],axis=1,inplace=True) #删除指定列 并且修改掉原始数据 # 删除td数据框中的 最后一列 ...
- 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid
[源码下载] 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid 作者:webabc ...