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.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
Source
Waterloo local 1999.09.25#include#include #include #include #define INF 0x3f3f3f3f using namespace std; const int maxn=500+10; int house[maxn][maxn],dis[maxn]; bool vis[maxn]; int m,n; void read_Graph() { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(i==j) house[i][j]=0; else house[i][j]=house[j][i]=INF; } for(int i=1;i<=m;i++) { int u; scanf("%d",&u); dis[u]=0; vis[u]=true; } } void floyd() { for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(house[i][j]>house[i][k]+house[k][j]) house[i][j]=house[i][k]+house[k][j]; } int main() { int u,v,w; scanf("%d%d",&m,&n); { memset(vis,false,sizeof(vis)); memset(dis,INF,sizeof(dis)); read_Graph(); while(~scanf("%d%d%d",&u,&v,&w)) house[u][v]=house[v][u]=min(w,house[u][v]); floyd(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(vis[j]) dis[i]=min(dis[i],house[i][j]); int tmp,ans,ans_dis=INF; for(int i=1;i<=n;i++) { tmp=-1; for(int j=1;j<=n;j++) tmp=max(min(dis[j],house[i][j]),tmp); if(tmp