程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj2607Fire Station(floyd最短路)

poj2607Fire Station(floyd最短路)

編輯:C++入門知識

poj2607Fire Station(floyd最短路)


題目鏈接:

啊哈哈,點我帶我

這道題目當時一看覺得很熟悉,但是後來越想越混亂,搞得最後題目都沒搞清楚。。。比賽的時候不知道怎麼想的,但是大致思想是對的。。。。
題意:
這道題目是講原來鎮上有若干個加油站,但是鎮上的居民覺得消防站的距離李自己家太遠,所以決定在居民點鍵一個消防站,要使離居民點的最大距離最小。。
思路:毫無疑問是最短路。。。但是這題數據太多。。所以預處理的時候用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

Waterloo local 1999.09.25

代碼為:

#include
#include
#include
#include
#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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved