Fire Station

Time Limit: 5000ms
Memory Limit: 65536KB

This 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 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

Source

 
解题:枚举最短路,坑爹,居然文件结束输入路径
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
const int maxn = ;
struct arc{
int to,cost,next;
arc(int x = ,int y = ,int z = -){
to = x;
cost = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],d[maxn],dd[maxn];
int tot,n,m;
bool done[maxn];
void add(int u,int v,int w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
e[tot] = arc(u,w,head[v]);
head[v] = tot++;
}
void dijkstra(int s,int *d){
for(int i = ; i <= n; ++i) done[i] = false;
d[s] = ;
priority_queue< pii,vector< pii >,greater< pii > >q;
q.push(make_pair(d[s],s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] > d[u] + e[i].cost){
d[e[i].to] = d[u] + e[i].cost;
q.push(make_pair(d[e[i].to],e[i].to));
}
}
}
}
int main(){
while(~scanf("%d %d",&m,&n)){
int fire[maxn],tmp,u,v,w;
bool isfire[maxn] = {false};
memset(head,-,sizeof(head));
for(int i = tot = ; i < m; ++i){
scanf("%d",&tmp);
fire[i] = tmp;
isfire[tmp] = true;
}
while(~scanf("%d %d %d",&u,&v,&w)) add(u,v,w);
for(int i = ; i <= n; ++i) d[i] = INF;
for(int i = ; i < m; ++i) dijkstra(fire[i],d);
int maxv = INF,index = ;
for(int i = ; i <= n; ++i){
if(isfire[i]) continue;
memcpy(dd,d,sizeof(d));
dijkstra(i,dd);
int mmxx = ;
for(int k = ; k <= n; ++k)
mmxx = max(mmxx,dd[k]);
if(mmxx < maxv){
index = i;
maxv = mmxx;
}
}
printf("%d\n",index);
}
return ;
}

POJ 2607 Fire Station的更多相关文章

  1. POJ 2607 Fire Station(Floyd打表+枚举更新最优)

    题目链接: http://poj.org/problem?id=2607 Description A city is served by a number of fire stations. Some ...

  2. Nyoj Fire Station

    描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...

  3. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  4. POJ 2607

    一次FLOYD,再枚举. 注意题目要求的输出是什么哦. #include <iostream> #include <cstdio> #include <cstring&g ...

  5. [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)

    Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...

  6. poj - 4045 - Power Station

    题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...

  7. POJ 2152 Fire(树形dp)

    http://poj.org/problem?id=2152 题意: n个节点组成的树,要在树一些点上建立消防站,每个点建站都有个cost[i],每个点如果不在当前的点上建站,也要依赖其他的消防站,并 ...

  8. POJ 2152 Fire(树形DP)

    题意: 思路:令F[i][j]表示 的最小费用.Best[i]表示以i为根节点的子树多有节点都找到负责消防站的最小费用. 好难的题... #include<algorithm> #incl ...

  9. POJ 2152 Fire

    算是我的第一个树形DP 的题: 题目意思:N个城市形成树状结构.现在建立一些消防站在某些城市:每个城市有两个树形cost(在这个城市建立消防站的花费),limit : 我们要是每个城镇都是安全的:就是 ...

随机推荐

  1. 学习SCSS

    目录 变量 嵌套 引入 混合 继承 操作符 CSS扩展 嵌套属性 标签(空格分隔): 未分类 变量 变量用来存储需要在CSS中复用的信息,例如颜色和字体.SASS通过$符号去声明一个变量. $font ...

  2. HDU 1047 Integer Inquiry( 高精度加法水 )

    链接:传送门 思路:高精度水题 /************************************************************************* > File ...

  3. phpMyAdmin 高级功能尚未完全设置,部分功能未激活(转载)

    phpMyAdmin 高级功能尚未完全设置,部分功能未激活.请点击这里查看原因. 第一步: 使用Mysql管理员帐号通过phpmyadmin登陆,然后点击“导入”,然后点击“浏览”按钮,找到phpmy ...

  4. C++递归方法实现全排列

    #include<iostream> using namespace std; void perm(int list[],int k,int m);//声明 void perm(int l ...

  5. Linux用shell链接上传文件

    yum install lrzsz 安装lrzsz ,直接拖拽到黑框框就可以上传了 或者使用 rz 命令,会弹出选择文件的框框

  6. Linux进程的内存布局

    这张图很好,注意其中最上面是高位地址,虽然很多个0,但是c开头的,不要看反了: 更具体的可以看这里: A.正文段.这是由cpu执行的机器指令部分.通常,正文段是可共享的,所以即使是经常执行的程序(如文 ...

  7. WinForm容器内控件批量效验是否同意为空?设置是否仅仅读?设置是否可用等方法分享

    WinForm容器内控件批量效验是否同意为空?设置是否仅仅读?设置是否可用等方法分享 在WinForm程序中,我们有时须要对某容器内的全部控件做批量操作.如批量推断是否同意为空?批量设置为仅仅读.批量 ...

  8. ThinkPHP5.0框架开发--第7章 TP5.0数据库操作

    ThinkPHP5.0框架开发--第7章 TP5.0数据库操作 第7章 TP5.0数据库操作 ===================================================== ...

  9. ElasticSearch Shard——本质上是做分布式扩展,副本对于集群的稳定性有很强的影响

    什么是一个Shard? Shard就是一个Lucene Index,参照文章(深入理解Shard和Lucene Index). Index需要多少个Shard? 回答这个问题,我们需要先谈谈节点,一个 ...

  10. 将枚举存入map集合

    遍历枚举存入集合: Map<String,Object> deptLevel = new HashMap<>(); for(OrgBussinessEnum orgBussin ...