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;
}


你可能感兴趣的:(http://poj.org/problem?id=2607&&最短路)