PAT甲级 1072 Gas Station(30) (Dijkstrla)

题目

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.

输入

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.

输出

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.

样例输入 

样例输入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
样例输入2:

2 1 2 10
1 G1 9
2 G1 20

样例输出 

样例输出1:

G1
2.0 3.3
样例输出2:

No Solution

题意理解

有一些G开头的地名 这些都是加油站我们要找出一个加油站与距离它最近的房屋的距离尽可能远。并且所有的点都必须在这个加油站的服务范围以内。如果有多个这样的点,选取位置与所有房屋的平均距离最小,再相同选择编号最小的点。

我们选择暴力跑Dijkstrla 将所有加油站的点都放入后面也当作一个点,如果最后没有解决方案的话那就输出No Solution 如果有的话我们要更新 其实我们就是将加油站看成特殊的点,以每个加油站点跑最短路,然后暴力分析每个点的平均距离,如果有更优的那么更新全局的。

代码 

#include<bits/stdc++.h>
using namespace std;
const int N=2e3+10;
const int INF=0x3f3f3f3f;
int dis[N];
bool st[N];
int n,m,k,D_se;
int g[N][N];
int get(string s){//将字符串换成数字 stoi函数
    if(s[0]=='G')return n+stoi(s.substr(1));//从1取子串取到结尾
    else return stoi(s);
}
int res=-1,mind=0,sumd=INF;
void dijkstra(int u,int &mind1,int &sumd1){
    memset(dis,INF,sizeof dis);
    memset(st,0,sizeof st);
    dis[u]=0;
    for(int i=1;i<=n+m;i++){
        int t=-1;
        for (int j=1;j<=n+m;j++)
            if (!st[j]&&(t==-1||dis[t]>dis[j]))
                t=j;

        for (int j=1;j<=n+m;j++)
            dis[j]=min(dis[j],dis[t]+g[t][j]);
        st[t]=1;
    }
    for(int i=1;i<=n;i++){
        if(dis[i]>D_se){
            mind1=-1;
            return;
        }
    }
    mind1=INF,sumd1=0;
    for(int i=1;i<=n;i++){
       mind1=min(mind1,dis[i]);
       sumd1+=dis[i];
    }
}
int main(){
    scanf("%d%d%d%d",&n,&m,&k,&D_se);
    memset(g,INF,sizeof g);
    while(k--){
          string a,b;
          cin>>a>>b;
          int c;
          scanf("%d",&c);
          int x=get(a),y=get(b);
          g[x][y]=g[y][x]=min(g[x][y],c);
    }

    for(int i=n+1;i<=n+m;i++){
           int mind1,sumd1;
           dijkstra(i,mind1,sumd1);
           if(mind1>mind)res=i,mind=mind1,sumd=sumd1;
           else if(mind1==mind&&sumd1<sumd)res=i,sumd=sumd1;
    }
    if(res==-1)puts("No Solution");
    else {
        printf("G%d\n",res-n);
        printf("%.1lf %.1lf\n",(double)mind,(double)sumd/n);
    }
    return 0;
}


版权声明:本文为NHS6671原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。