poj2607Fire Station(floyd最短路)

来源:互联网 发布:php新视频教程 编辑:程序博客网 时间:2024/05/05 17:28

题目链接:

啊哈哈,点我带我

这道题目当时一看觉得很熟悉,但是后来越想越混乱,搞得最后题目都没搞清楚。。。比赛的时候不知道怎么想的,但是大致思想是对的。。。。
题意:
这道题目是讲原来镇上有若干个加油站,但是镇上的居民觉得消防站的距离李自己家太远,所以决定在居民点键一个消防站,要使离居民点的最大距离最小。。
思路:毫无疑问是最短路。。。但是这题数据太多。。所以预处理的时候用floyd求出两个
任意两点间的距离,然后用加油站去更新到到各个居民点的最短距离。。那么枚举所有加油站后就会得到所有到居民点离最近的消防站的最短距离,最后在枚举所有的居民点,把居民点假设为消防站,然后找到居民点离这个虚拟加油站的最大值,然后每次枚举一个居民点后,如果得到的最大值比先前得到对的最大值小,那么就可以更新这个值,并保存这个居民点。。。最后得到的就是最优解。。。。还有一个很坑的就是输入居民点之间的距离的时候是一直输入到文件结尾。。所以输入完后要加ctrl+z才能得到结果。。。。。。这是我第一次碰到这样的输入。。。。
题目:

Fire Station
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3749 Accepted: 1324

Description

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. 

Input

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.

Output

You are to output a single integer: 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.

Sample Input

1 621 2 102 3 103 4 104 5 105 6 106 1 10

Sample Output

5

Source

Waterloo local 1999.09.25

代码为:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxn=500+10;int house[maxn][maxn],dis[maxn];bool vis[maxn];int m,n;void read_Graph(){    for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)        {            if(i==j)  house[i][j]=0;            else  house[i][j]=house[j][i]=INF;        }        for(int i=1;i<=m;i++)        {            int u;            scanf("%d",&u);            dis[u]=0;            vis[u]=true;        }}void floyd(){    for(int k=1;k<=n;k++)            for(int i=1;i<=n;i++)             for(int j=1;j<=n;j++)                if(house[i][j]>house[i][k]+house[k][j])                      house[i][j]=house[i][k]+house[k][j];}int main(){    int u,v,w;    scanf("%d%d",&m,&n);    {        memset(vis,false,sizeof(vis));        memset(dis,INF,sizeof(dis));        read_Graph();        while(~scanf("%d%d%d",&u,&v,&w))            house[u][v]=house[v][u]=min(w,house[u][v]);        floyd();        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)               if(vis[j])                 dis[i]=min(dis[i],house[i][j]);        int tmp,ans,ans_dis=INF;        for(int i=1;i<=n;i++)        {            tmp=-1;            for(int j=1;j<=n;j++)                 tmp=max(min(dis[j],house[i][j]),tmp);            if(tmp<ans_dis)            {                ans_dis=tmp;                ans=i;            }        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击