1. 程式人生 > >PAT A1072. Gas Station

PAT A1072. Gas Station

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS

, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.

Sample Input 1:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:

No Solution

/**************************侯尼瑪**************************/

一道簡單的圖演算法題,我的做法是對每個Candidate節點做一次迪傑斯特拉演算法,然後選擇最優的節點。唯一需要注意的一點是這道題的節點選擇條件比較繞:

(1)Candidate節點到每個house的最近距離都必須大於服務範圍。

(2)從滿足條件1的Candidate節點中選擇離全部house的最近距離最遠的那個(就像是在現實生活中建工廠,選址的時候肯定得在保證能服務到全部住戶的情況 下離住戶儘可能得遠)

(3)從滿足條件2的Candidate節點中選擇離全部house的平均距離最近的那個(這道題有一個問題,就是題幹中的測試例輸出的平均值是經過四捨五入的,但是提交的程式碼中輸出的平均值得直接去尾而不能四捨五入,否則第二個測試例不能通過。不知道是題目出錯了還是我的程式碼有什麼漏洞)

最後附上程式碼:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;

const int INF = 0x3FFFFFFF;

bool Dijstra (int ** vertex,double &MinDistance,double &AverageDistance,int HouseNum,int CandidNum,int CandidLocal,int ServeRange)
{
    int LocalNum = HouseNum+CandidNum;
    bool * LocalVisited = new bool [LocalNum+1]();
    int * PaceValue = new int [LocalNum+1];
    for(int i=1;i<=LocalNum;i++)
        PaceValue[i]=INF;
    PaceValue[CandidLocal]=0;
    for(int i= 0;i<=LocalNum;i++)
    {
        int MinPace=INF;
        int s=-1;
        for(int j=1;j<=LocalNum;j++)
            if(PaceValue[j]<MinPace && LocalVisited[j]==false)
            {
                s=j;
                MinPace=PaceValue[j];
            }
        if (s==-1) break;
        LocalVisited[s]=true;
        for(int t=1;t<=LocalNum;t++)
        {
            if(vertex[s][t]!=INF && LocalVisited[t]==false)
                if(vertex[s][t]+PaceValue[s]<PaceValue[t])
                    PaceValue[t]=vertex[s][t]+PaceValue[s];
        }

    }

    double SumDistance=0;
    for(int i=1;i<=HouseNum;i++)
    {
        if(PaceValue[i]>ServeRange)
            return false;
        if(PaceValue[i]<MinDistance)
            MinDistance=PaceValue[i];
        SumDistance+=(double)PaceValue[i];
    }

    AverageDistance=SumDistance/HouseNum;

    return true;
}

int main()
{
    int HouseNum,CandidNum,EdgeNum,ServeRange;
    while (scanf("%d %d %d %d",&HouseNum,&CandidNum,&EdgeNum,&ServeRange)!=EOF)
    {
        int LocalNum = HouseNum+CandidNum;
        int ** vertex = new int *[LocalNum+1];
        for(int i =1;i<=LocalNum;i++)
        {
            vertex[i]=new int [LocalNum+1];
            for(int j=1;j<=LocalNum;j++)
                vertex[i][j]=INF;
        }
        string STRLoc1,STRLoc2;
        int INTLoc1,INTLoc2,TempValue;
        //int tempEdge =0;
        for(int k=0;k<EdgeNum;k++)
        {
            cin>>STRLoc1>>STRLoc2>>TempValue;
            INTLoc1=INTLoc2=0;
            if(STRLoc1[0]=='G')
            {
                for(size_t s=1;s<STRLoc1.size();s++)
                    INTLoc1=INTLoc1*10+(STRLoc1[s]-'0');
                INTLoc1+=HouseNum;
            }
            else
                for(size_t s=0;s<STRLoc1.size();s++)
                    INTLoc1=INTLoc1*10+(STRLoc1[s]-'0');
            if(STRLoc2[0]=='G')
            {
                for(size_t s=1;s<STRLoc2.size();s++)
                    INTLoc2=INTLoc2*10+(STRLoc2[s]-'0');
                INTLoc2+=HouseNum;
            }
            else
                for(size_t s=0;s<STRLoc2.size();s++)
                    INTLoc2=INTLoc2*10+(STRLoc2[s]-'0');
            if(TempValue<vertex[INTLoc1][INTLoc2])
                vertex[INTLoc1][INTLoc2]=vertex[INTLoc2][INTLoc1]=TempValue;
        }
        bool SupplyAll = false;
        double MinDistanceAll = 0;
        double AverageDistanceAll = INF;
        int ChoosedLocal = INF;

        for(int i=1;i<=CandidNum;i++)
        {
            double MinDistance = INF;
            double AverageDistance = INF;
            bool ThisSupply = Dijstra(vertex,MinDistance,AverageDistance,HouseNum,CandidNum,HouseNum+i,ServeRange);

            if(ThisSupply)
            {
                SupplyAll=true;
                if(MinDistance>MinDistanceAll)
                {
                     ChoosedLocal=HouseNum+i;
                     MinDistanceAll= MinDistance;
                     AverageDistanceAll=AverageDistance;
                }
                else if(MinDistance==MinDistanceAll && AverageDistance<AverageDistanceAll)
                {
                     ChoosedLocal=HouseNum+i;
                     AverageDistanceAll=AverageDistance;
                }

            }
        }
        if(SupplyAll)
        {
            cout<<'G'<<ChoosedLocal-HouseNum<<endl;
            printf("%d.0 %.1f",(int)MinDistanceAll,AverageDistanceAll);
        }
        else cout<<"No Solution"<<endl;

    }
    return 0;
}