Question 236.pat Class A Exercise - 1072 Gas Station (30 points)


Question 236.pat Class A Exercise - 1072 Gas Station (30 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

This question requires a gas station in the best location. The shortest distance from the gas station to the houses needs to be the largest among all gas stations, that is, to find the maximum and minimum distance (as can be seen from the sentence in the question) : 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. Because I misunderstood the problem at the time, I put it here to remind myself of my illness. . . ). The idea is as follows:
① Each time the gas station is traversed in a loop, first use the heap-optimized dijkstra to find the shortest distance from the gas station to each house.
②Loop through the above shortest distance to see if there is any beyond the service range, if so, the gas station will not be considered. In the above process, at the same time, the shortest shortest distance is obtained, and the summation is made for the use of the average path length in the subsequent sequence.
③ Each time traverse the cycle of the gas station to find the largest and shortest shortest distance, and update the gas station as the answer. Note that when there is a choice of gas stations that are the same as the largest, the shortest, and the shortest distance, the average road length of the two will be considered, and the shortest one will be chosen as the answer. Since it is considered from the gas station with the smaller number, there is no need to worry about the case where the gas station with the smaller number is the answer when the average road length is equal.

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;
const int add=1000;//因为房子从1编号到1000,所以1000之后没有被编号,那就用来给加油站编号
const int maxn=add+11;//加上加油站,编号共编到1010
const int Inf=0x3f3f3f3f;

struct Node
{
    
    
    int index;
    int dist;
    bool operator < (const Node &a) const
    {
    
    
        return dist>a.dist;
    }
};

int N,M,K,Ds;
priority_queue<Node> pq;
vector<pii> G[maxn];
int collected[maxn];
int dist[maxn];
int bestpos;
double maxminpath,minaverpath=Inf;
int flag;

void Dijkstra(int s)
{
    
    
    fill(dist,dist+maxn,Inf);
    fill(collected,collected+maxn,0);
    dist[s]=0;
    pq.push(Node{
    
    s,0});
    while(!pq.empty())
    {
    
    
        int minv=pq.top().index;
        pq.pop();
        if(collected[minv])
        {
    
    
            continue;
        }
        collected[minv]=1;
        for(int i=0;i<(int)G[minv].size();i++)
        {
    
    
            int v=G[minv][i].first;
            int w=G[minv][i].second;
            if(!collected[v]&&dist[v]>dist[minv]+w)//满足松弛条件,做松弛操作,并将新权放入pq
            {
    
    
                dist[v]=dist[minv]+w;
                pq.push(Node{
    
    v,dist[v]});
            }
        }
    }
}

int main()
{
    
    
    cin>>N>>M>>K>>Ds;
    for(int i=0;i<K;i++)
    {
    
    
        int u,v,w;
        string str_u,str_v;
        cin>>str_u>>str_v>>w;
        if(str_u[0]=='G')
        {
    
    
            u=stoi(str_u.substr(1,str_u.length()-1))+add;
        }
        else
        {
    
    
            u=stoi(str_u);
        }
        if(str_v[0]=='G')
        {
    
    
            v=stoi(str_v.substr(1,str_v.length()-1))+add;
        }
        else
        {
    
    
            v=stoi(str_v);
        }
        G[u].push_back({
    
    v,w});
        G[v].push_back({
    
    u,w});
    }
    for(int i=1;i<=M;i++)
    {
    
    
        Dijkstra(i+add);
        int j;
        double pathsum=0,averpath,nowminpath=Inf;
        for(j=1;j<=N;j++)
        {
    
    
            if(dist[j]>Ds)
            {
    
    
                break;
            }
            pathsum+=dist[j];
            nowminpath=min(nowminpath,(double)dist[j]);
        }
        if(j>N)
        {
    
    
            averpath=pathsum/N;
            flag=1;
        }
        else
        {
    
    
            continue;
        }
        if(nowminpath>maxminpath)//本题要的是加油站到house中的最大最小距离,即这个最小距离要在所有的最小距离中是最大的
        {
    
    
            minaverpath=averpath;
            maxminpath=nowminpath;
            bestpos=i;
        }
        else if(nowminpath==maxminpath)
        {
    
    
            if(averpath<minaverpath)//其次考虑加油站到houses的平均距离要最小
            {
    
    
                minaverpath=averpath;
                bestpos=i;
            }
        }
    }
    if(!flag)
    {
    
    
        printf("No Solution");
    }
    else
    {
    
    
        printf("G%d\n",bestpos);
        printf("%.1lf %.1lf",maxminpath,minaverpath);
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324347602&siteId=291194637