PAT A1072. Gas Station

来源:互联网 发布:js扫描枪事件 编辑:程序博客网 时间:2024/05/20 11:47

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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2
Sample Output 1:
G12.0 3.3
Sample Input 2:
2 1 2 101 G1 92 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;}


0 0
原创粉丝点击