PAT-A 真题- 1072. Gas Station

发布于 / PAT-甲级 / 0 条评论

原题干:

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

题目大意:规划加油站的建设点!

有N的居民房,M个加油站待建点,K条边,要求所有居民房距离加油站不得超过DS

要求加油站在不超过DS的情况下距离居民房越远越好。让你找出距离最远的加油站

如果这样最远的加油站存在多个,找出距离居民房平均距离最近的一个

如果仍然存在多个,找出编号最小的一个

注意,加油站点的编号由G1, G2, G3...表示,居民房编号由1,2,3...表示

输入第一行给出N  M  K  DS

接着给出K行,分别表示 顶点1  顶点2  距离(边权)

输出要求输出加油站点编号,另起一行输出加油站距居民房最短距离 和 加油站距离居民房的平均距离,以空格隔开。

这道题可以使用Dijkstra来做,枚举加油站,对于每个加油站为起点使用Dijkstra算法计算居民房与之最短距离,再使用一个函数算出这个距离即可

因为要使用多次Dijkstra算法,所以要特别注意初始化操作。

注意:加油站编号Gn,可能是两位数的G10,最好使用stoi来写,不能用str[1] - '0'。否则最后一个测试点过不去

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1050, inf = 0x7fffffff;
int HPointCnt, GPointCnt, RoadCnt, MaxRange, PointCnt;
int G[maxn][maxn], mindist[maxn];

int Str2Num(string s){
    if(s[0] == 'G') {
        s = s.substr(1);
        return HPointCnt + stoi(s);
    }else
        return stoi(s);
}

void Dijkstra(int s){
  fill(mindist, mindist + maxn, inf);
  bool v[maxn] = {false};
  mindist[s] = 0;
  for(int i = 1; i <= PointCnt; i++){
    int p = -1, min = inf;
    for(int j = 1; j <= PointCnt; j++)
      if(!v[j] && mindist[j] < min)
        min = mindist[j], p = j;
    if(p == -1) return;
    v[p] = true;
    for(int j = 1; j <= PointCnt; j++)
      if(!v[j] && G[p][j] != inf && mindist[p] + G[p][j] < mindist[j])
        mindist[j] =  mindist[p] + G[p][j];
  }
}

bool cal(int & min_dist, double & avg_dist){
  int dist = 0, min = inf;
  for(int i = 1; i <= HPointCnt; i++){
    if(mindist[i] < min) min = mindist[i];
    dist += mindist[i];
    if(mindist[i] > MaxRange) return false;
  }
  min_dist = min, avg_dist = 1.0 * dist / HPointCnt;
  return true;
}

int main(){
  fill(G[0], G[0] + maxn * maxn, inf);
  cin >> HPointCnt >> GPointCnt >> RoadCnt >> MaxRange;
  PointCnt = HPointCnt + GPointCnt;
  for(int i = 0; i < RoadCnt; i++){
    string l, r;
    cin >> l >> r;
    int _l = Str2Num(l), _r = Str2Num(r);
    cin >> G[_l][_r];
    G[_r][_l] = G[_l][_r];
  }
  int this_min_dist, min_dist = -1, ojbk_point = -1;
  double this_avg_dist, avg_dist;
  for(int i = 1; i <= GPointCnt; i++){
    Dijkstra(i + HPointCnt);
    if(cal(this_min_dist, this_avg_dist)){
      if(min_dist < this_min_dist){
        min_dist = this_min_dist;
        avg_dist = this_avg_dist;
        ojbk_point = i;
      }else if(min_dist == this_min_dist && avg_dist > this_avg_dist){
        avg_dist = this_avg_dist;
        ojbk_point = i;
      }
      else if(min_dist == this_min_dist && avg_dist == this_avg_dist)
        continue;
    }
  }
  if(ojbk_point == -1) cout << "No Solution" << endl;
  else{
    cout << "G" << ojbk_point << endl;
    printf("%.1f %.1f\n", 1.0 * min_dist, avg_dist);
  }
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1072. Gas Station
目前还没有评论,快来抢沙发吧~