POJ2607:Fire Station(SPFA+枚举)

博客围绕村庄新建消防站选址问题展开。已知有f个已建好的消防站,要新建一个使离最远村庄的距离最小。解题思路是先求各村庄到已建消防站的最短距离,再枚举每个村庄,计算在此建消防站后的最短路,根据最大距离更新选址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意分析:

有f个已经建好的消防站,在村庄里再建一个消防站,要求离最远的村庄的距离最小。

解题思路:

先对于每一个已经建好的消防站,求出村庄离消防站的最短距离,再枚举每一个村庄,在这个村庄建消防站后求最短路,再看最大距离是否变小,更新消防站的位置。

#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1020
int a[N], dis[N], dis1[N], book[N], inf=99999999;
struct edge {
	int v;
	int w;
};

vector<edge>e[N];
void SPFA(int u)
{
	int i, v, w;
	dis[u] = 0;
	queue<int>q;
	memset(book, 0, sizeof(book));
	q.push(u);
	book[u] = 1;
	while (!q.empty())
	{
		u = q.front();
		q.pop();
		book[u] = 0;
		for (i = 0; i < e[u].size(); i++) 
		{
			v = e[u][i].v;
			w = e[u][i].w;
			if (dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				if (book[v] == 0)
				{
					book[v] = 1;
					q.push(v);
				}
			}
		}
	}
}
int main()
{
	int n, q, i, j, u, v, w, num, ans, maxi;
	while(scanf("%d%d", &q, &n)!=EOF)
	{
		for (i = 1; i <= n; i++)
			e[i].clear();
		for (i = 0; i < q; i++)
		{
			scanf("%d", &a[i]);
		}
		while (scanf("%d%d%d", &u, &v, &w) != EOF)
		{
			e[u].push_back(edge{v, w});
			e[v].push_back(edge{ u, w });
		}
		for (i = 1; i <= n; i++)
			dis[i] = inf;
		for (i = 0; i < q; i++)
			SPFA(a[i]);
		maxi = 0;
		for (i = 1; i <= n; i++)
		{
			dis1[i] = dis[i];
			maxi = max(maxi, dis[i]);
		}
		ans = 1;
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= n; j++)
				dis[j] = dis1[j];
			SPFA(i);
			num = 0;
			for (j = 1; j <= n; j++)
				num = max(num, dis[j]);
			if (num < maxi)
			{
				ans = i;
				maxi = num;
			}
				
		}
		printf("%d\n", ans);
	}	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值