zoukankan      html  css  js  c++  java
  • PAT甲级——1072 Gas Station

    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 (≤), the total number of houses; M (≤), the total number of the candidate locations for the gas stations; K(≤), 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

    即对每个加油站都是用Dijkstra计算最小距离
     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #define inf 999999999
     5 using namespace std;
     6 int N, M, K, Ds, indexG = -1;
     7 double minG = -1, avgG;
     8 int city[1010][1010];
     9 int main()
    10 {
    11     cin >> N >> M >> K >> Ds;
    12     fill(city[0], city[0] + 1010 * 1010, inf);
    13     for (int i = 0; i < K; ++i)
    14     {
    15         string s1, s2;
    16         int a = 0, b = 0, dis;
    17         cin >> s1 >> s2 >> dis;
    18         if (s1[0] == 'G')
    19         {
    20             a += N;
    21             s1.erase(0, 1);
    22         }
    23         if (s2[0] == 'G')
    24         {
    25             b += N;
    26             s2.erase(0, 1);
    27         }
    28         a += stoi(s1);
    29         b += stoi(s2);
    30         city[a][b] = city[b][a] = dis;
    31     }
    32     //Dijsktra
    33     for (int k = N+1; k <= N+M; ++k)//每个加油站都是用一次dij
    34     {
    35         int temp[1010];
    36         fill(temp, temp + 1010, inf);
    37         bool visit[1010];
    38         fill(visit, visit + 1010, false);
    39         temp[k] = 0;
    40         for (int i = 1; i <= N+M; ++i)
    41         {
    42             int v = -1, minDis = inf;
    43             for (int j = 1; j <= N+M; ++j)
    44             {
    45                 if (visit[j] == false && minDis > temp[j])
    46                 {
    47                     v = j;
    48                     minDis = temp[j];
    49                 }
    50             }
    51             if (v == -1)break;
    52             visit[v] = true;
    53             for (int u = 1; u <= N+M; ++u)
    54             {
    55                 if (visit[u] == false && city[v][u] != inf)
    56                 {
    57                     if (temp[u] > temp[v] + city[v][u])
    58                         temp[u] = temp[v] + city[v][u];
    59                 }
    60             }
    61         }
    62         int flag = 1, minD = inf;
    63         double avgD = 0.0;
    64         for (int i = 1; i <= N; ++i)
    65         {
    66             minD = minD < temp[i] ? minD : temp[i];
    67             avgD += (double)temp[i];
    68             if (temp[i] > Ds)
    69             {
    70                 flag = 0;
    71                 break;
    72             }
    73         }
    74         avgD /= N;
    75         if (flag == 1 && minG <= minD)
    76         {
    77             if ((minG < minD) || (minG == minD && avgD < avgG))
    78             {
    79                 minG = minD;
    80                 indexG = k;
    81                 avgG = avgD;
    82             }
    83             else if (minG == minD && avgD == avgG)
    84                 indexG = indexG < k ? indexG : k;
    85         }
    86     }
    87     if (indexG == -1)
    88         cout << "No Solution" << endl;
    89     else
    90         printf("G%d
    %.1f %.1f
    ", indexG - N, minG, avgG);
    91     return 0;
    92 }
  • 相关阅读:
    【GO】GO开发环境安装及VS Code配置
    【计算机组成原理】第一章 计算机系统概论
    oracle 12.2新特性:sqlplus history
    取消交互式创建linux用户密码
    undo表空间收缩
    如何对undo表空间大小进行设置
    查看数据库实际使用多大内存
    Oracle 12.2Cgrid脚本安装失败软件完全删除
    有关rman备份ora-27192和ora-19511错误
    数据库altert日志中的GTX提示
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11312473.html
Copyright © 2011-2022 走看看