描述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. 《Java程序书面采访猿收藏》之 instanceof的作用是什么

    instanceof它是Java二元运算语言,它的作用是推断对象变量引用被指向的类型是一类(或接口.抽象类.父亲)示例.即,对象是否是它的一个实例离开它的类的权.返回boolean数据类型. 常见的使 ...

  2. C++实现堆排序

    堆排序是合并排序和插入排序排序方法共同的长处.它的时间复杂度O(nlgn),这也是一个地方排序算法:在任何时候,外阵中拥有唯一不变的输入数组存储的元素.引进第一家引进什么样的堆堆. 1.建堆:堆数据结 ...

  3. SP2010 3D标签云Web部分--很酷的效果,强烈推荐!!

    SP2010 3D标签云Web部分--很酷的效果.强烈推荐! ! 项目描述叙事         基于简单Flash的3D标签云Web部件.SP Server 2010使用. 建立在内置标签云Web部件 ...

  4. TinyXml高速入口(一)

    笔者:朱金灿 来源:http://blog.csdn.net/clever101 对于xml文件,眼下我的工作仅仅是集中在配置文件和作为简单的信息文件来用.因此我不太喜欢使用msxml这样的重量级的x ...

  5. Shell在大数据的魅力时代:从一点点思路百度大数据面试题

    供Linux开发中的同学们,Shell这可以说是一个基本功. 对于同学们的操作和维护.Shell也可以说是一种必要的技能,Shell.对于Release Team,软件配置管理的同学来说.Shell也 ...

  6. Memcahce(MC)系列(两)Linux下一个Memcache安装

    Linux下一个memcache安装 memcache是高性能.分布式的内存对象缓存系统,用于在动态应用中降低数据库负载.提升訪问速度.眼下用memcache解决互联网上的大用户读取是很流行的一种使用 ...

  7. 谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比較

    谈谈CListCtrl 扩展风格设置方法 --------------------------------------SetExtendedStyle和ModifyStyleEx 比較 对于刚開始学习 ...

  8. CentOS7.0 安装JAVA周围环境

    CentOS7.0 安装JAVA周围环境  安装JDK 1.配置JDK环境变量 把下载好的JDK(jdk-7u75-linux-x64.gz)文件上传到 Reg: /home/p2pweb/java/ ...

  9. unity3d脚本

    一 创建和使用脚本 1 概述 GameObject的行为都是被附加到其上面的组件控制,脚本本质上也是一个组件. 在unity中创建一个脚本.默认内容例如以下: using UnityEngine; u ...

  10. iPhone发展【一】从HelloWorld开始

    转载请注明出处.原文网址:http://blog.csdn.net/m_changgong/article/details/8013553 作者:张燕广 从经典的HelloWorld開始踏入iPhon ...