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 ... 
随机推荐
- MVC Razor标签
			1. RenderBody在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句:@Rend ... 
- 安卓的sqlite增删改
			基于安卓的sqlite增删改,笔记学习: 1.使用LinearLayout 布局生成,增删改的页面如图 代码布局如下: <LinearLayout xmlns:android="htt ... 
- Dictionary带来的一种隐式内存泄漏
			当心Dictionary带来的一种隐式内存泄漏 最近在看Dictionary的源代码的时候, 突然想到Dictionary的不当使用中有一种隐含内存泄漏的可能. 简化使用场景 小A正在写一个简单的图书 ... 
- jQuery Validate插入 reomte使用详细的说明
			在用户注冊时常常要通过ajax请求推断用户账号是否已注冊,最方便的方法便是用jQuery Validate插件 reomte方法 Jquery Validate插件, 调用远程方法验证參数, remo ... 
- NYOJ 745 蚂蚁问题(两)
			蚂蚁的难题(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 下雨了,下雨了.蚂蚁搬家了. 已知有n种食材须要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种 ... 
- jQuery
代码的层定位滑动动画效果
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ... 
- Oracle 六闪回技术,flashback
			Flashback 技术基于Undo segment基于内容的, 因此,限制UNDO_RETENTON参数. 要使用flashback 特征,您必须启用自己主动撤销管理表空间. 在Oracle 11g ... 
- 系统如何端子app弄root才干
			最近由于调试USB OTG怪东西.这导致USB端口被占用,这项.虽然我是project版本号,但不能运行adb shell,这是不可能的debug该. 所以,我现在是一个系统终端apk,规划 http ... 
- Effective Java (7) - 避免终止方法
			一. 基本概念 1. 所谓的终结方法事实上是指finalize(). 2. Java的垃圾回收机制仅仅负责内存相关清理.其它资源的清理(释放文件.释放DB连接)须要程序猿手动完毕. 3. 调用Syst ... 
- 泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)
			专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo crazyi(天下无雪 ... 
