Poj 2607 Fire Station

Source: Internet
Author: User
Fire stationtime limit: 5000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 2607
64-bit integer Io format: % LLD Java class name: Main 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 varous 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.
Inputthe 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. outputyou are to output a single INTEGER: the lowest intersection number at which a new fire station shoshould be 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
Sourcewaterloo local problem-solving: enumeration is the shortest-path.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 #define pii pair<int,int>16 using namespace std;17 const int maxn = 2050;18 struct arc{19     int to,cost,next;20     arc(int x = 0,int y = 0,int z = -1){21         to = x;22         cost = y;23         next = z;24     }25 };26 arc e[maxn*maxn];27 int head[maxn],d[maxn],dd[maxn];28 int tot,n,m;29 bool done[maxn];30 void add(int u,int v,int w){31     e[tot] = arc(v,w,head[u]);32     head[u] = tot++;33     e[tot] = arc(u,w,head[v]);34     head[v] = tot++;35 }36 void dijkstra(int s,int *d){37     for(int i = 1; i <= n; ++i) done[i] = false;38     d[s] = 0;39     priority_queue< pii,vector< pii >,greater< pii > >q;40     q.push(make_pair(d[s],s));41     while(!q.empty()){42         int u = q.top().second;43         q.pop();44         if(done[u]) continue;45         done[u] = true;46         for(int i = head[u]; ~i; i = e[i].next){47             if(d[e[i].to] > d[u] + e[i].cost){48                 d[e[i].to] = d[u] + e[i].cost;49                 q.push(make_pair(d[e[i].to],e[i].to));50             }51         }52     }53 }54 int main(){55     while(~scanf("%d %d",&m,&n)){56         int fire[maxn],tmp,u,v,w;57         bool isfire[maxn] = {false};58         memset(head,-1,sizeof(head));59         for(int i = tot = 0; i < m; ++i){60             scanf("%d",&tmp);61             fire[i] = tmp;62             isfire[tmp] = true;63         }64         while(~scanf("%d %d %d",&u,&v,&w)) add(u,v,w);65         for(int i = 0; i <= n; ++i) d[i] = INF;66         for(int i = 0; i < m; ++i) dijkstra(fire[i],d);67         int maxv = INF,index = 1;68         for(int i = 1; i <= n; ++i){69             if(isfire[i]) continue;70             memcpy(dd,d,sizeof(d));71             dijkstra(i,dd);72             int mmxx = 0;73             for(int k = 1; k <= n; ++k)74                 mmxx = max(mmxx,dd[k]);75             if(mmxx < maxv){76                 index = i;77                 maxv = mmxx;78             }79         }80         printf("%d\n",index);81     }82     return 0;83 }
View code

 

Poj 2607 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.