Nyoj Fire Station
描述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的更多相关文章
- POJ 2607 Fire Station(Floyd打表+枚举更新最优)
题目链接: http://poj.org/problem?id=2607 Description A city is served by a number of fire stations. Some ...
- POJ 2607 Fire Station
Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- [DLX反复覆盖] hdu 3656 Fire station
题意: N个点.再点上建M个消防站. 问消防站到每一个点的最大距离的最小是多少. 思路: DLX直接二分推断TLE了. 这时候一个非常巧妙的思路 我们求的距离一定是两个点之间的距离 因此我们把距离都求 ...
- zoj 3820 Building Fire Stations 树的中心
Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- zoj 3820 Building Fire Stations (二分+树的直径)
Building Fire Stations Time Limit: 5 Seconds Memory Limit: 131072 KB Special Judge Marjar ...
- 牡丹江.2014B(图论,树的直径)
B - Building Fire Stations Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & ...
- NUC_HomeWork1 -- POJ2067(最短路)
C - Fire Station Description A city is served by a number of fire stations. Some residents have comp ...
- CSUFT2016训练赛
解题报告: Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 39958 Accepted: 13 ...
- 【转】Dancing Links题集
转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...
随机推荐
- 破解win2008r2服务器域用户名
启动PE系统 进入 cmd窗口 cd 进入 win2008r2服务器的安装盘(假设为d:) d: cd windows/system32 ren osk.exe osk02.exe #重命令屏幕键盘 ...
- 设计师给了px显着的单位,Android要设置多少开发商dip、dp、sp?
此链接 http://blog.csdn.net/xiaodongrush/article/details/29560431 1. 要开发一款Android APP,设计师和开发要约定哪些事情? ...
- mapreduce程序来实现分类
文件的内容例如以下所看到的: 5 45 8 876 6 45 要求最后的输出格式: 1 5 2 6 3 8 4 45 5 45 5 876 首先,这个题目是须要对文 ...
- Android的ViewAnimator而它的子类ViewSwitcher-android学习之旅(三十三)
ViewAnimator遗传FrameLayout,重合使用多个组件.可以增加部件数量,然后会有时间切换动画. ViewAnimator及其子类的继承关系 ViewAnimator经常使用属性 Vie ...
- UML简单梳理类图
依赖 Dependency Class Car{} Class Person{ int a; static int b public void buy(Car c){ int c; .... } } ...
- Windows下Git服务器搭建[转]
Windows下Git服务器搭建 作为对前两天Git服务器搭建的一个整理,我想分别从服务端和客户端两个角度来记录下整个搭建过程,为了达到目标,我们需要哪些操作. (一)服务端软件和账号的安装配置 ...
- ubuntu下一个jboss-seam-2.2.2.Final/examples/build.xml:754: warning: 'includeantruntime' was not set
[javac] /home/huihui/app/jboss-seam-2.2.2.Final/examples/build.xml:754: warning: 'includeantruntime' ...
- 【剑指offer】第一个字符只出现一次
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27106997 题目描写叙述: 在一个字符串(1<=字符串长度<=10000,所 ...
- UVa10000_Longest Paths(最短路SPFA)
解题报告 求最长路. 用SPFA求最长路,初始化图为零,dis数组也为零 #include <iostream> #include <cstdio> #include < ...
- android--jenkins+gradle+android自动化构建apk步骤(转)
第一步,安装jenkins,这个网上教程挺多的. 第二步,下载并配置gradle.下载地址http://www.gradle.org/ 解压gradle至某路径下,如/usr/local/lib/gr ...