码迷,mamicode.com
首页 > 其他好文 > 详细

poj2607Fire Station(floyd最短路)

时间:2014-08-14 01:35:37      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   文件   

题目链接:

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

题目:

Fire Station
Time Limit: 5000MS   Memory Limit: 65536K
Total 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 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10

Sample Output

5

Source


代码为:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using 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;
}





poj2607Fire Station(floyd最短路),布布扣,bubuko.com

poj2607Fire Station(floyd最短路)

标签:des   style   http   color   os   io   strong   文件   

原文地址:http://blog.csdn.net/u014303647/article/details/38545815

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!