POJ 2607 Fire Station(floyd)


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



题意:已知城市和已建加油站(健在一部分城市),求下一个加油站健在哪里,改城市到加油站最远距离最小



我已无力吐槽这个题了,这个题的坑真是坑死人了,首先那个输入很害人,然后居然用floyd算法,我惊呆了,
看到小伙伴都做出来了,我却wa了11发(第几斯特拉 算法 +误解题意+超时),这个人都不好了,
比赛完看到别人代码,居然还还看不懂,感觉图论太差了(⊙o⊙)…哎。。。。。太菜了

细节我都注释在代码中了(仿  lyf   代码)


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000
#define maxe 0x3f3f3f3f

int a[N][N],dis[N];
int b[N];
int n,m;


void inset()
{
    int i,j;
    for(i=1;i<=m;i++)
        for(j=1;j<=m;j++)
         if(i==j)
            a[i][j]=0;
         else
            a[i][j]=a[j][i]=maxe;
}

void floyd()  // 不懂就百度floyd
{
    int i,j,k;

    for(k=1;k<=m;k++)
        for(i=1;i<=m;i++)
           for(j=1;j<=m;j++)
              if(a[i][j]>a[i][k]+a[k][j])
                   a[i][j]=a[i][k]+a[k][j];
}

void solve()
{
    int i,j,cur=0;
    int minn;

    for(i=1;i<=m;i++)
    {
        minn=maxe;
        for(j=0;j<n;j++)
        {
            if(a[i][b[j]]<minn)
                minn=a[i][b[j]];
        }
        dis[i]=minn;  //dis[i]保存的是点i到所有加油站最近的距离
        cur=max(cur,dis[i]);  //cur保存的是开始时所有点到加油站最小距离的最大值
    }

    int pos=1,temp; //至今没有想清楚为什么pos不赋值为1为什么错
  
  
    for(i=1;i<=m;i++)  //设i为加油站
    {
        temp=0;
        for(j=1;j<=m;j++)
            if(a[i][j]<dis[j])  //因为j到新加油站i的距离比以前所有加油站距离小
              temp=max(temp,a[i][j]);
            else
              temp=max(temp,dis[j]); //我强调一下,temp是当以i为加油站后,所有点到已知加油站
                                     //距离最小的最大值

        if(temp<cur) 
        {
            cur=temp;
            pos=i;
        }
    }

    printf("%d\n",pos);
}
int main()
{
    int i;
    scanf("%d%d",&n,&m);

    for(i=0;i<n;i++)
        scanf("%d",&b[i]);

    inset();

    int x,y,len;

    while(~scanf("%d%d%d",&x,&y,&len))//坑爹的地方
    {
        if(a[x][y]>len)
          a[x][y]=a[y][x]=len;
    }

    floyd();

    solve();

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值