首页 > 代码库 > POJ 2607 Fire Station
POJ 2607 Fire Station
Fire Station
Time Limit: 5000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 260764-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 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.
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 621 2 102 3 103 4 104 5 105 6 106 1 10
Sample Output
5
Source
Waterloo local 1999.09.25
解题:枚举最短路,坑爹,居然文件结束输入路径
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 }
POJ 2607 Fire Station
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。