描述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 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.

Subsequent test cases are separated with a single blank line.

The number of test cases are less than 200.

输出
You are to output a single integer for each test case: 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.
样例输入
1 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
样例输出
5
来源
University of Waterloo Local Contest 199
上传者
张云聪

无奈...自己的程序POJ可以过....理工的过不去...POJ(可以用Floyd...水过)

标程代码:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define maxN 510
#define MAX 0x0fffffff
int max(int x,int y){return x>y?x:y;}
struct point{
int v,nex,w;
}po[maxN*maxN];
int num,N,M,head[maxN],disY[maxN],disX[maxN],key[maxN],flag[maxN];
bool vis[maxN];
void insert(int u,int v,int w)
{
po[num].v=v;
po[num].w=w;
po[num].nex=head[u];
head[u]=num++;
}
void spfa(int soure,int* dis)
{
memset(vis,false,sizeof(vis));
queue<int>q;
q.push(soure);
vis[soure]=true;dis[soure]=;
while(!q.empty())
{
int u=q.front();q.pop();vis[u]=false;
for(int i=head[u];i!=-;i=po[i].nex)
if(dis[po[i].v]>dis[u]+po[i].w){
dis[po[i].v]=dis[u]+po[i].w;
if(!vis[po[i].v]){
q.push(po[i].v);
vis[po[i].v]=true;
}
}
}
}
int main()
{
//freopen("1.txt","r",stdin);
char s[];
while(~scanf("%d%d",&M,&N))
{
getchar();
int i,j,u,v,w,k=,minn=MAX;
num=;
memset(head,-,sizeof(head));
memset(flag,,sizeof(flag));
for(i=;i<=M;i++){scanf("%d",&key[i]);getchar();}
if(N==){printf("1\n");continue;}
while(gets(s)!=NULL&&strlen(s)){sscanf(s,"%d%d%d",&u,&v,&w);insert(u,v,w);insert(v,u,w);}
for(i=;i<=N;i++)disX[i]=MAX;
for(i=;i<=M;i++)spfa(key[i],disX);
for(j=;j<=N;j++){
int maxx=;
if(disX[j]==)continue;
for(i=;i<=N;i++)disY[i]=disX[i];
spfa(j,disY);
for(i=;i<=N;i++)maxx=max(maxx,disY[i]);
if(minn>maxx||maxx==){k=j;minn=maxx;}
}
printf("%d\n",k);
}
}

POJ:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 501
#define INF 0x3f3f3f3f
int n,m,cost[maxn][maxn],dis[maxn],vis[maxn];
int main()
{
while(~scanf("%d%d",&m,&n))
{
int u,v,c;
for(int i=;i<=n;i++)
{
dis[i]=INF;vis[i]=;
for(int j=;j<=n;j++)
cost[i][j]=i==j?:INF;
}
for(int i=;i<=m;i++)
{
scanf("%d",&u);
dis[u]=;
vis[u]=;
}
while(~scanf("%d%d%d",&u,&v,&c))
cost[u][v]=cost[v][u]=c;
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
for(int i=;i<=n;i++)
if(vis[i])
for(int j=;j<=n;j++)
dis[j]=min(dis[j],cost[i][j]);
int ans=INF,pos;
for(int i=;i<=n;i++)
{
int temp=-;
for(int j=;j<=n;j++)
temp=max(temp,min(dis[j],cost[i][j]));
if(ans>temp)ans=temp,pos=i;
}
printf("%d\n",pos);
}
return ;
}

Nyoj 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. POJ 2607 Fire Station

    Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  3. [DLX反复覆盖] hdu 3656 Fire station

    题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...

  4. zoj 3820 Building Fire Stations 树的中心

    Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  5. zoj 3820 Building Fire Stations (二分+树的直径)

    Building Fire Stations Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge Marjar ...

  6. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

  7. NUC_HomeWork1 -- POJ2067(最短路)

    C - Fire Station Description A city is served by a number of fire stations. Some residents have comp ...

  8. CSUFT2016训练赛

    解题报告: Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 39958   Accepted: 13 ...

  9. 【转】Dancing Links题集

    转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...

随机推荐

  1. zerglurker的c语言教程006——第一功能

    行,以往的经验教训后,.成员main性能.变数.命名等基本概念应该有一个初步的了解 下面,我们就可以开始我们自己的第一个定义的函数. 仿照头等舱.操作的第二个教训.添加一个新的项目的解决方案Lessi ...

  2. cocos2dx-3.0(13)------SpriteBatchNode与SpriteFrameCache渲染速度

    大家都知道一个游戏里面会有大量的图片,每一个图片渲染是须要时间的,以下分析两个类来加快渲染速度,加快游戏执行速度          一.SpriteBatchNode          1.先说下渲染 ...

  3. 认识Backbone (四)

    Backbone.View(视图) 视图的核心是处理数据业务逻辑.绑定DOM元素事件.渲染模型或者集合数据. 添加DOM元素  render view.render() render 默认实现是没有操 ...

  4. Kafka设计

    [Apache Kafka]Kafka设计   在开始开发producer和consumer之前,先从设计的角度看一看Kafka. 由于重度依赖JMS,且实现方式各异.对可伸缩架构的支持不够,Link ...

  5. java中用正則表達式推断中文字符串中是否含有英文或者数字

    public static boolean includingNUM(String str)throws  Exception{ Pattern p  = Pattern.compile(" ...

  6. OpenCV:Mat元素访问方法、演出、代码的复杂性和安全性分析

    欢迎转载.尊重原创,因此,请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种訪问矩阵元素的方法, ...

  7. oracle 创建字段自增长——两种实现方式汇总(转)

    mysql等其他数据库中有随着记录的插入而表ID自动增长的功能,而oracle却没有这样的功能,我们有以下两种方式可以解决字段自增长的功能. 因为两种方式都需要通过创建序列来实现,这里先给出序列的创建 ...

  8. ReferenceTypeDemo

    对象a作为参数argument在方法中使用时,如果argument在方法中赋予另一个对象的地址,则之后方法中对参数argument的操作,都不会影响到对象a. 方法中参数argument如果是对象,a ...

  9. 设置SQLServer数据库中某些表为只读的多种方法

    原文:设置SQLServer数据库中某些表为只读的多种方法 翻译自:http://www.mssqltips.com/sqlservertip/2711/different-ways-to-make- ...

  10. 使用live delegate on解决js后装html故障问题

    今天写一个前端的东西.每学到更多的知识.几下就能写几行代码.代码行数十个.代码几个文件量--这是真的.一直以来研究的前端遇到的问题仍然在实践百度谷歌问答. 我今天遇到这样的问题:已经写js代码,正确a ...