http://poj.org/problem?id=2607&&最短路

Fire Station
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3169 Accepted: 1130

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
题意:为了尽可能的减少居民离消防站的不满意度(所有居民离其最近的消防站距离最大值),现在决定让你选一个新的地址使得不满意度最小,,
思路:先对当前已经存在的消防站求最短路径,然后枚举没有建消防站的地址,求最短路,找出最小的那个即为新选的地址。。。
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
#include<string>
#include<cstdio>
#define inf 0xffffff
#define M 505
using namespace std;
int len[M],tmplen[M];
struct Gnode
{
	Gnode() {}
	Gnode(int len,int num):len(len),num(num) {}
	int len,num;
};
vector<int>fire;
void SPFA(const vector<vector<Gnode> >&Graph,int start)
{
	queue<int> Q;
	Q.push(start);
	len[start]=0;
	while(!Q.empty())
	{
		int cur=Q.front();Q.pop();
		for(int i=0;i<Graph[cur].size();++i)
		{
			if(len[cur]!=inf&&len[cur]+Graph[cur][i].len<len[Graph[cur][i].num])
			{
				len[Graph[cur][i].num]=len[cur]+Graph[cur][i].len;
				Q.push(Graph[cur][i].num);
			}
		}
	}
}
int main()
{
	int f,n;
	while(~scanf("%d%d",&f,&n))
	{
		vector<vector<Gnode> >Graph(n);
		fire.clear();
		bool visit[M];
	    memset(visit,false,sizeof(visit));
		for(int i=0;i!=f;++i)
		{
			int a;
			scanf("%d",&a);
			fire.push_back(a-1);
			visit[a-1]=true;
		}
		getchar();
		char str[100];
		while(gets(str)&&strlen(str))
		{  int a,b,len;
			sscanf(str,"%d%d%d",&a,&b,&len);
			Graph[a-1].push_back(Gnode(len,b-1));
			Graph[b-1].push_back(Gnode(len,a-1));
		}
		for(int i=0;i<n;++i) len[i]=inf;
		for(int i=0;i<fire.size();++i)
		  SPFA(Graph,fire[i]);
		copy(len,len+n,tmplen);
		int p,min=inf;
		for(int i=0;i!=n;++i)
			if(!visit[i])
	      {
				SPFA(Graph,i);
				  int maxx=*max_element(len,len+n);
				if(maxx<min){p=i;min=maxx;}
			    copy(tmplen,tmplen+n,len);
			}
		printf("%d\n",p+1);
	}return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值