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 ...
随机推荐
- FPGA机器学习之学习的方向
经过了2个月对机器学习的了解后.我发现了,机器学习的方向多种多样.网页排序.语音识别,图像识别,推荐系统等.算法也多种多样.看见其它的书后,我发现除了讲到的k均值聚类.贝叶斯,神经网络,在线学习等等, ...
- [Eclipse]代码已被写入关于如何切换到unix在新行
切换换行符的显示格式, 一般的设置是这种: 可是这样仅仅能对新文件起作用,原来已经写好的还是那熊样.(怎么办呢,得吃懊悔药啊,谁让你開始没准备好呢?!T_T) 以下就是懊悔药: 这样就OK了. 附:有 ...
- effective c++ 条款10 handle assignment to self operator =
非强制性,但是个好习惯 当使用连锁赋值时很有用 x=y=z=10; class Window { public: Window& operator=(int size) { ... retur ...
- 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...
- HDU4866 Shooting (要持久段树)
意甲冠军: 给你一些并行x行轴.总是询问坐标x的顶部之前,k一个段高度,.标题是必须在线. 思路: 首先要会可持久化线段树(又称主席树和函数式线段树).不会的能够去做下POJ 2104. 把全部线段高 ...
- node.js基础:数据存储
无服务器的数据存储 内存存储 var http = require('http'); var count = 0; //服务器访问次数存储在内存中 http.createServer(function ...
- 并发编程实践三:Condition
Condition实例始终被绑定到一个锁(Lock)上.Lock替代了Java的synchronized方法,而Condition则替代了Object的监视器方法,包含wait.notify和noti ...
- Atitit.Hibernate于Criteria 使用汇总and 关系查询 and 按照子对象查询 o9o
Atitit.Hibernate于Criteria 使用总结and 关联查询 and 依照子对象查询 o9o 1. Criteria,,Criterion ,, 1 <2. 基本的对象黑头配置磊 ...
- HDU2516-取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 【Andrioid】在Gradle编译时生成一个不同的版本号,动态设置应用程序标题,应用程序图标,更换常数
写项目的时候常常会遇到下面的情况: 1.须要生成測试版本号和正式版本号的apk 2.測试版本号和正式版本号的URL是不一样的 3.測试版本号和正式版本号的包名须要不一致,这样才干安装到同一部手机上面. ...