# 10278 FireStation

每个结点都有人居住

每个结点可能没有消防站、或者有1或多个消防站

现在有居民抱怨离最近的消防站的距离都太远了

所以决定新建一些消防站

You are to output a single integer: the lowest intersection number at which a new fire station shouldbe built so as to minimize the maximum distance from any intersection to the nearest fire station.

这n个距离里一定有一个最大值max。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define INF 9999

int f, n,have[110],g[510][510],d[510];

void Floyd()
{
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (g[i][j] > g[i][k] + g[k][j])
g[j][i]=g[i][j] = g[i][k] + g[k][j];

}

int findMax(int a[])
{
int m = 1;
for (int i = 2; i <= n; i++)
if (a[i] > a[m])
m = i;
return m;
}

int main()
{

freopen("c:\\data\\10278.txt", "r", stdin);

int t;
scanf("%d", &t);
while (t--)
{
char s[5];
gets(s);

scanf("%d %d", &f, &n);

memset(have, 0, sizeof(0));
//已经存在的防火墙
for (int i = 1; i <= f; i++)
{
int p;
scanf("%d", &p);
have[p]++;
}

memset(g, INF, sizeof(g));
//路的长度
for (int i = 1; i <= n; i++)
{
g[i][i] = 0;
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
g[b][a]=g[a][b] = c;
}

//求任意两点的最短路径
Floyd();

//枚举所有可能
int newPos;
int min=INF;
for (int i = 1; i <= n; i++)
{
have[i]++;
//每个点到最近的防火站的距离
for (int j = 1; j <= n; j++)
{
int dis = INF;

for (int k = 1; k <= n; k++)
if (have[k] > 0&&dis>g[j][k])
dis = g[j][k];

d[j] = dis;
}
int p = findMax(d);
if (d[p] < min) {
min = d[p]; newPos = i;
}

have[i]--;
}

printf("%d\n", newPos);
if (t)printf("\n");
}

return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：10278 FireStation 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)