Fire Station
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.
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題目:尋找一個站點建立救火站,使得所有點到救火站的最短距的最大值能更小。
代碼:
#include<stdio.h>
#define maxn 511
#define inf 1 << 29
#define Max(a, b) a > b ? a : b
#define Min(a, b) a < b ? a : b
int map[maxn][maxn];
int p[maxn];
int sh[maxn];
void set(int n)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
map[i][j] = inf;
}
map[i][i] = 0;
}
}
void floyd(int n)
{
int i, j, k;
for (k = 1; k <= n; k++)
{
for (i = 1; i <= n; i++)
{
if (i != k)
{
for (j = 1; j <= n; j++)
{
if (j != i && j != k && map[i][j] > map[i][k] + map[k][j])
{
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
}
}
int deal(int n, int f)
{
int min, max, pre;
int i, j, ans;
for (i = 1, pre = 0; i <= n; i++)//求出每個頂點到現有的最近救火站距離保存于sh
{
for (j = 0, min = inf; j < f; j++)
{
min = Min(min, map[i][p[j]]);
}
sh[i] = min;
pre = Max(pre, min);//挑出最短距地最大值
}
for (i = 1, ans = i; i <= n; i++)//枚舉每個節點
{
for (j = 1, max = 0; j <= n; j++)
{
if (map[i][j] < sh[j])//如果j到i的距離更短
{
max = Max(max, map[i][j]);
}
else
{
max = Max(max, sh[j]);
}
}
if (pre > max)//如果最大值被更新,則表示在i建立一個救火站能是最短距地最大值更小
{
ans = i;
pre = max;
}
}
return ans;
}
int main()
{
int n, i, f, u, v;
int w;
scanf("%d%d", &f, &n);
{
set(n);
for (i = 0; i < f; i++)
{
scanf("%d", &p[i]);
}
while (scanf("%d%d%d", &u, &v, &w) != EOF)
{
map[u][v] = w, map[v][u] = w;
}
floyd(n);
printf("%d\n", deal(n, f));
}
system("pause");
return 0;
}
#define maxn 511
#define inf 1 << 29
#define Max(a, b) a > b ? a : b
#define Min(a, b) a < b ? a : b
int map[maxn][maxn];
int p[maxn];
int sh[maxn];
void set(int n)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
map[i][j] = inf;
}
map[i][i] = 0;
}
}
void floyd(int n)
{
int i, j, k;
for (k = 1; k <= n; k++)
{
for (i = 1; i <= n; i++)
{
if (i != k)
{
for (j = 1; j <= n; j++)
{
if (j != i && j != k && map[i][j] > map[i][k] + map[k][j])
{
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
}
}
int deal(int n, int f)
{
int min, max, pre;
int i, j, ans;
for (i = 1, pre = 0; i <= n; i++)//求出每個頂點到現有的最近救火站距離保存于sh
{
for (j = 0, min = inf; j < f; j++)
{
min = Min(min, map[i][p[j]]);
}
sh[i] = min;
pre = Max(pre, min);//挑出最短距地最大值
}
for (i = 1, ans = i; i <= n; i++)//枚舉每個節點
{
for (j = 1, max = 0; j <= n; j++)
{
if (map[i][j] < sh[j])//如果j到i的距離更短
{
max = Max(max, map[i][j]);
}
else
{
max = Max(max, sh[j]);
}
}
if (pre > max)//如果最大值被更新,則表示在i建立一個救火站能是最短距地最大值更小
{
ans = i;
pre = max;
}
}
return ans;
}
int main()
{
int n, i, f, u, v;
int w;
scanf("%d%d", &f, &n);
{
set(n);
for (i = 0; i < f; i++)
{
scanf("%d", &p[i]);
}
while (scanf("%d%d%d", &u, &v, &w) != EOF)
{
map[u][v] = w, map[v][u] = w;
}
floyd(n);
printf("%d\n", deal(n, f));
}
system("pause");
return 0;
}

