POJ 2607 Fire Station(Floyd打表+枚举更新最优)
2018-05-24 08:45
369 查看
题目链接:
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
Sample Output
Source
Waterloo local 1999.09.25
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
Waterloo local 1999.09.25
1 /*
2 问题
3 给出已经是消防站的个数和每个消防站在哪个顶点上,给出以EOF结尾的边
4 计算并输出新建一个消防站,使得各个站点距离自己最近的消防站的距离最小,比如样例中选择新建在5号,使得各个顶点距离自己最近的
5 消防站的距离最远是10,而选择在其他点时有的站点距离自己最近的消防站比10大,所以选择5号顶点,另外就是可以选择已经有消防站的
6 点新建,因为题目要有新建的顶点的标号尽量小。
7
8 解题思路
9 500个站点使用Floyd算出各个顶点到各个顶点的最短距离,方便之后枚举新站点时直接查表得到两点的最短距离,然后依次枚举,找出各个
10 站点距离自己最近的消防站的最大距离,更新最优,最后输出标号即可。
11 */
12 #include<cstdio>
13 #include<cstring>
14 #include<algorithm>
1516 using namespace std;
17 const int maxn=600;
18 const int INF=99999999;
19 int e[maxn][maxn],book[maxn],ans[maxn],m;
20
21 void Floyd();
22
23 int main()
24 {
25 int f,t1,i,j,t2,t3,t4;
26 while(scanf("%d%d",&f,&m) != EOF){
27 memset(book,0,sizeof(book));
28 for(i=1;i<=m;i++)
29 ans[i]=INF;
30
31 for(i=0;i<f;i++){
32 scanf("%d",&t1);
33 book[t1]=1;//标记是消防站
34 }
3536 for(i=1;i<=m;i++){
37 for(j=1;j<=m;j++){
38 e[i][j] = i==j?0:INF;
39 }
40 }
41
42 while(scanf("%d%d%d",&t2,&t3,&t4) != EOF){
43 e[t2][t3]=e[t3][t2]=min(t4,e[t2][t3]);
44 }
45 Floyd();
46
47 //ans数组表示各个站点距离离自己最近的消防站的距离
48 for(i=1;i<=m;i++){
49 if(book[i])
50 ans[i]=0;
51 else
52 for(j=1;j<=m;j++){
53 if(book[j])
54 ans[i]=min(ans[i],e[i][j]);
55 }
56 }
57 /*for(i=1;i<=m;i++){
58 printf("%d ",ans[i]);
59 }
60 printf("\n");*/
61
62 int ansmin=INF;
63 int biaohao;
64 for(i=1;i<=m;i++){//枚举每个站点是消防站,找到该点是新建消防站时距离该点最远的点并记录距离,如果该距离是最小的
65 //记录该点最后输出
66 int temp=-1;
67 for(j=1;j<=m;j++)
68 temp = max(min(e[i][j],ans[j]),temp);
69
70 if(ansmin > temp){
71 ansmin = temp;
72 biaohao=i;
73 }
74 }
7576 printf("%d\n",biaohao);
77 }
78 return 0;
79 }
80
81 void Floyd()
82 {
83 int i,j,k;
84 for(k=1;k<=m;k++)
85 for(i=1;i<=m;i++)
86 for(j=1;j<=m;j++)
87 if(e[i][j] > e[i][k]+e[k][j])
88 e[i][j] = e[i][k]+e[k][j];
89 }
相关文章推荐
- poj 2607 Fire Station(floyd)
- POJ 2607 Fire Station(Floyd)
- POJ 2607 Fire Station(floyd)
- POJ 2607 / ZOJ 1857 /NYOJ210 Fire Station
- POJ 1178 - Camelot (枚举+dp : floyd)
- POJ 2607 Fire Station
- poj 3660 传递性关系 floyd更新
- ZOJ-1857(POJ-2607) Fire Station
- POJ 2240 【这题貌似可以直接FLOYD 屌丝用SPFA通过枚举找正权值环 顺便学了下map】
- poj 2607 Fire Station (spfa)
- poj 1178 Camelot floyd + 枚举
- POJ 2607 / ZOJ 1857 Fire Station (临接表+spfa)
- poj 2607 Fire Station
- POJ 2607 Fire Station 消防站
- POJ 2886 Who Gets the Most Candies?【线段树单点更新+反素数打表】【好题】
- POJ 2112 Optimal Milking(多重匹配+floyd+二分枚举)
- POJ 3369 Grocery store (枚举 打表)
- poj2886 Who Gets the Most Candies?(单点更新+约瑟夫环+素数打表)
- ZOJ 1857 && POJ 2607 Fire Station【SPFA】
- poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】