Nyoj Fire Station

Source: Internet
Author: User

Description A city is served by a number of fire stations. Some residents has complained that the distance from their houses to the nearest station was too far, so a new station is To is built. The location of the choose, the distance to the nearest station from the houses of The disgruntled residents.

The city have up to $ intersections, connected by road segments of various lengths. No more than road segments intersect at a given intersection. The location of houses and firestations alike is considered to being at intersections (the travel distance from the Intersec tion to the actual building can be discounted). Furthermore, we assume that there are at least one house associated with every intersection. There may is more than one firestation per intersection.

Input
The first line of input contains II positive integers:f,the number of existing fire stations (f <=) and I, the Nu Mber of intersections (I <= 500). The intersections is numbered from 1 to I consecutively. f lines follow; Each contains the intersection number at which a existing fire station is found. A number of lines follow, each containing three positive integers:the number of a intersection, the number of a differen T intersection, and the length of the road segment connecting the intersections. All road segments is two-way (at least as far as fire engines is concerned), and there would exist a route between any PA IR of intersections.

Subsequent test cases is separated with a single blank line.

The number of test cases is less than 200.
Output
You is to
output a single integer for each test case:the lowest intersection number @ which a new fire station SH Ould is built so as to minimize the maximum distance from any intersection to the nearest fire station.
Sample input
1 621 2 102 3 103 4 104 5 105 6 106 1 10
Sample output
5
Source
University of Waterloo Local Contest 199
Uploaded by
Zhang Yunzun

Helpless... Own program POJ can be too .... Science and technology ... POJ (can be used Floyd ... Water over)

Standard Code:

1  2#include <iostream>3#include <stdio.h>4#include <string.h>5#include <queue>6 using namespacestd;7 #defineMAXN 5108 #defineMAX 0X0FFFFFFF9 intMaxintXintY) {returnX>y?x:y;}Ten structpoint{ One     intv,nex,w; A}po[maxn*MAXN]; - intNUM,N,M,HEAD[MAXN],DISY[MAXN],DISX[MAXN],KEY[MAXN],FLAG[MAXN]; - BOOLVIS[MAXN]; the voidInsertintUintVintW) - { -po[num].v=v; -po[num].w=W; +po[num].nex=Head[u]; -head[u]=num++; + } A voidSPFA (intSoure,int*dis) at { -memset (Vis,false,sizeof(Vis)); -queue<int>Q; - Q.push (soure); -vis[soure]=true;d is[soure]=0; -      while(!q.empty ()) in     { -         intU=q.front (); Q.pop (); vis[u]=false; to          for(inti=head[u];i!=-1; i=Po[i].nex) +             if(dis[po[i].v]>dis[u]+PO[I].W) { -dis[po[i].v]=dis[u]+PO[I].W; the                 if(!VIS[PO[I].V]) { * Q.push (PO[I].V); $vis[po[i].v]=true;Panax Notoginseng                 } -             } the     } + } A intMain () the { +     //freopen ("1.txt", "R", stdin); -     Chars[ -]; $      while(~SCANF ("%d%d",&m,&N)) $     { - GetChar (); -         inti,j,u,v,w,k=1, minn=MAX; thenum=0; -memset (head,-1,sizeof(head));Wuyimemset (Flag,0,sizeof(flag)); the          for(i=1; i<=m;i++) {scanf ("%d",&key[i]); GetChar ();} -         if(n==1) {printf ("1\n");Continue;} Wu          while(Gets (s)!=null&&strlen (s)) {sscanf (s),"%d%d%d",&u,&v,&W); insert (u,v,w); insert (v,u,w);} -          for(i=0; i<=n;i++) disx[i]=MAX; About          for(i=1; i<=m;i++) SPFA (KEY[I],DISX);  $          for(j=1; j<=n;j++){ -             intmaxx=0;  -             if(disx[j]==0)Continue; -              for(i=1; i<=n;i++) disy[i]=Disx[i]; A SPFA (j,disy); +              for(i=1; i<=n;i++) maxx=Max (maxx,disy[i]); the             if(minn>maxx| | maxx==0) {k=j;minn=Maxx;}  -         } $printf"%d\n", k); the     } the}

POJ:

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5 using namespacestd;6 #defineMAXN 5017 #defineINF 0x3f3f3f3f8 intN,M,COST[MAXN][MAXN],DIS[MAXN],VIS[MAXN];9 intMain ()Ten { One      while(~SCANF ("%d%d",&m,&N)) A     { -         intu,v,c; -          for(intI=1; i<=n;i++) the         { -dis[i]=inf;vis[i]=0; -              for(intj=1; j<=n;j++) -Cost[i][j]=i==j?0: INF; +         } -          for(intI=1; i<=m;i++) +         { Ascanf"%d",&u); atdis[u]=0; -vis[u]=1; -         } -          while(~SCANF ("%d%d%d",&u,&v,&c)) -cost[u][v]=cost[v][u]=C; -          for(intk=1; k<=n;k++) in              for(intI=1; i<=n;i++)    -                  for(intj=1; j<=n;j++)    toCost[i][j]=min (cost[i][j],cost[i][k]+cost[k][j]); +          for(intI=1; i<=n;i++) -             if(Vis[i]) the                  for(intj=1; j<=n;j++) *dis[j]=min (dis[j],cost[i][j]); $         intans=Inf,pos;Panax Notoginseng          for(intI=1; i<=n;i++) -         { the             inttemp=-1; +              for(intj=1; j<=n;j++) Atemp=Max (Temp,min (dis[j],cost[i][j)); the             if(ans>temp) ans=temp,pos=i; +         } -printf"%d\n", POS); $     } $     return 0; -}
View Code

Nyoj Fire Station

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.