1072 Gas Station (30 分)--PAT甲级

1072 Gas Station (30 分)

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 (≤10​3​​), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10​4​​), the number of roads connecting the houses and the gas stations; and D​S​​, 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

题目大意:找出一个加油站,使得与所有住宅的最小距离最大,平均距离最小,同时每个住宅应在服务范围内。
候选的加油站相对住宅少很多,因此可以对每个候选站点进行一次dijkstra,对能做为加油站的站点计算最小距离和平均距离,选取最优解。

#include 
#include
#include
#include
#include 
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 1020;
int n,m,ds;
int dist[maxn][maxn];
bool visit[maxn];
int d[maxn];
int pre[maxn];
bool flag=true;//该站点能否被选上
void dijkstra(int s){
    fill(d+1,d+n+m+1,inf);
    fill(visit+1,visit+n+m+1,false);
    flag=true;
    d[s]=0;
    for(int i=1;i<=m+n;i++){
        int u=-1,MIN=inf;
        for(int j=1;j<=m+n;j++){
            if(visit[j]==false && d[j]<MIN){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1 || (MIN>ds && u<=n)) {
            flag=false;
            return;
        }
        visit[u]=true;
        for(int j=1;j<=m+n;j++){
            if(visit[j]==false && dist[u][j]!=0 && dist[u][j]+d[u]<d[j]){
                d[j]=dist[u][j]+d[u];
                pre[j]=u;
            }
        }
    }
}


int main()
{
    int k;
    scanf("%d %d %d %d",&n,&m,&k,&ds);
    for(int i=0;i<k;i++){
        string c1,c2;
        int s;
        cin>>c1>>c2>>s;
        int a,b;
        if(c1[0]=='G'){
            a=stoi(c1.substr(1))+n;
        }
        else a=stoi(c1);
        if(c2[0]=='G'){
            b=stoi(c2.substr(1))+n;
        }
        else b=stoi(c2);
        dist[a][b]=s;
        dist[b][a]=s;
    }
    int loc=0;
    int minsum=inf;
    int mindist=0;
    for(int i=1;i<=m;i++){
        dijkstra(i+n);
        if(flag){
            int temp=0;
            int mind=inf;
            for(int j=1;j<=n;j++){
                temp+=d[j];
                if(d[j]<mind) mind=d[j];
            }
            if((mind > mindist)||(mind==mindist && minsum > temp)){
                loc=i;
                minsum=temp;
                mindist=mind;
            }
        }
    }
    if(loc==0) printf("No Solution\n");
    else{
        float res=minsum*1.0/n;
        printf("G%d\n%d.0 %.1f",loc,mindist,res);
    }
    return 0;
}





你可能感兴趣的:(PAT)