• PAT 甲级 A1072 Gas Station


    Author CHEN, Yue

    Organization 浙江大学

    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:

    1. 4 3 11 5
    2. 1 2 2
    3. 1 4 2
    4. 1 G1 4
    5. 1 G2 3
    6. 2 3 2
    7. 2 G2 1
    8. 3 4 2
    9. 3 G3 2
    10. 4 G1 3
    11. G2 G1 1
    12. G3 G2 2

    Sample Output 1:

    1. G1
    2. 2.0 3.3

    Sample Input 2:

    1. 2 1 2 10
    2. 1 G1 9
    3. 2 G1 20

    Sample Output 2:

    No Solution

    题意:

    有n个居民楼,m个加油站,k条边(是无向图),DS是服务距离,然后要找出在所有加油站中,找出一个加油站,该加油站离居民楼尽可能地远(相对其他加油站),但是该距离又是和居民楼的最短距离设作dis,如果有多个加油站的dis相同,就比较平均距离,平均路径是该加油站到所有居民楼的最短路径之和的平均值,选最小的平均距离的加油站。

    思路:
    所有加油站使用dijstra算法找到居民楼的最短距离,找到最短距离中的最短距离,和其他的加油站的最短距离中的最短距离比较,找出最短距离中的最短距离的最长距离,这个就是dis,如果相等就比较平均距离。

    代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int maxn = 1020;
    6. const int INF = 1 << 30 - 1;
    7. int G[maxn][maxn];
    8. int d[maxn];
    9. bool vis[maxn] = {false};
    10. int n, m, k, DS;
    11. void Dijkstra(int st) {
    12. fill(d, d + maxn, INF);
    13. memset(vis, false, sizeof(vis));
    14. d[st] = 0;
    15. for(int i = 0; i < n + m; i++) {//单源最短路径,总顶点数要明确;
    16. int u = -1, MIN = INF;
    17. for(int j = 1; j <= n + m; j++) {
    18. if(vis[j] == false && d[j] < MIN) {
    19. u = j;
    20. MIN = d[j];
    21. }
    22. }
    23. if(u == -1) return ;
    24. vis[u] = true;
    25. for(int v = 1; v <= n + m; v++) {
    26. if(vis[v] == false && G[u][v] != INF) {
    27. if(d[u] + G[u][v] < d[v]) {
    28. d[v] = d[u] + G[u][v];
    29. }
    30. }
    31. }
    32. }
    33. }
    34. int getID(char s[]) {//给顶点编号;
    35. int len = strlen(s);
    36. int id = 0;
    37. int i = 0;
    38. //字符串转数字;
    39. while(i < len) {
    40. if(s[i] != 'G') {
    41. id = id * 10 + (s[i] - '0');
    42. }
    43. i++;
    44. }
    45. if(s[0] == 'G') return n + id;
    46. else {
    47. return id;
    48. }
    49. }
    50. int main () {
    51. scanf("%d %d %d %d", &n, &m, &k, &DS);
    52. //居民楼个数,加油站个数,无向图的边数,服务范围;
    53. fill(G[0], G[0] + maxn * maxn, INF);
    54. for(int i = 0; i < k; i++) {
    55. char city1[5], city2[5];
    56. int w;
    57. scanf("%s %s %d", &city1, &city2, &w);
    58. int u = getID(city1);
    59. int v = getID(city2);
    60. G[u][v] = G[v][u] = w;
    61. }//建图;
    62. double ansDis = -1, ansAvg = INF;//1,相对加油站中,离居民楼最远 2.相对居民楼,加油站和居民楼最近
    63. int ansID = -1;//赋值判断是否有解;
    64. for(int i = n + 1; i <= n + m; i++) {//遍历所有加油站;
    65. Dijkstra(i);
    66. double mindis = INF;//单源最短距离;
    67. double avg = 0;
    68. //遍历该加油站的最短距离;
    69. for(int j = 1; j <= n; j++) {//只需要遍历到居民楼的距离;
    70. if(d[j] > DS) {//超过服务距离,该服务站不合格;
    71. mindis = -1;
    72. break;
    73. }
    74. if(d[j] < mindis) {
    75. mindis = d[j];
    76. }
    77. avg += 1.0 * d[j] / n;
    78. }//找出最短距离和平均距离;
    79. if(mindis == -1) continue;
    80. if(mindis > ansDis) {
    81. ansDis = mindis;
    82. ansID = i;
    83. ansAvg = avg;
    84. }else if(mindis == ansDis && avg < ansAvg) {
    85. ansID = i;
    86. ansAvg = avg;
    87. }
    88. }
    89. if(ansID == -1) {
    90. printf("No Solution");
    91. }else {
    92. printf("G%d\n", ansID - n);
    93. printf("%.1f %.1f", ansDis, ansAvg);
    94. }
    95. return 0;
    96. }

    注意:
    测试点1,哪里写的是3.3,其实输出的是3.2也是对的(平均距离是3.25,保留一位就是3.2)

  • 相关阅读:
    计算电磁学(一)前置知识
    分布式锁之防止超卖 --mysql原子操作,乐观锁,redis事务,乐观锁
    第二证券|11天9板,“超级面料”概念火爆,高管却偷偷减持
    2022短视频神器历时6个月辛苦全力打造的软件短视频运用工具
    一文带你了解隐私 Layer1
    混合云中 DevOps 的最佳实践
    对HTML操作的工具类
    java实现权重随机获取值或对象
    Java面向对象进阶2——继承
    c++入门必学算法 快速幂
  • 原文地址:https://blog.csdn.net/m0_51711089/article/details/126148675