• 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 (≤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

    由于m很小(m<=10),所以可以枚举每一个加油站位置,每一个都是一个单源最短路径问题 ,使用Dijkstra记录距离,结束后遍历dis数组,只要有一个距离超过ds说明该答案不可行,最终对答案数组进行自定义排序,在该答案可行(flag==0)的前提下输出第一个即可,否则输出No Solution。

    具体见代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. //n小于等于1000,所以将加油站的编号+1000作为新编号,放入一张图中
    11. int dis[25][2020];//每个位置的加油站到每个房子的距离
    12. bool vis[2020];
    13. int n, m, k, ds, d, t1, t2;
    14. string x, y;
    15. int ans;//答案编号
    16. struct edge {
    17. int next, dis;
    18. };
    19. priority_queueint, int>, vectorint, int>>, greaterint, int>>>q;
    20. vectorg[2020];
    21. struct Ind {
    22. double minn, avg; //最小距离,平均距离
    23. int indexx;//编号
    24. bool flag;//答案是否可行, 0可行
    25. } a[20];
    26. bool cmp(Ind i1, Ind i2) {
    27. return i1.minn == i2.minn ? (i1.avg == i2.avg ? i1.indexx < i2.indexx : i1.avg < i2.avg) : i1.minn > i2.minn;
    28. }
    29. int main() {
    30. cin >> n >> m >> k >> ds;
    31. for (int i = 0; i < k; i++) {
    32. cin >> x >> y >> d;
    33. t1 = 0, t2 = 0;
    34. if (x[0] == 'G') {
    35. for (int j = 1; j < x.size(); j++) {
    36. t1 = t1 * 10 + x[j] - '0';
    37. }
    38. t1 += 1000;
    39. } else {
    40. t1 = atoi(x.c_str());
    41. }
    42. if (y[0] == 'G') {
    43. for (int j = 1; j < y.size(); j++) {
    44. t2 = t2 * 10 + y[j] - '0';
    45. }
    46. t2 += 1000;
    47. } else {
    48. t2 = atoi(y.c_str());
    49. }
    50. edge e;
    51. e.next = t2;
    52. e.dis = d;
    53. g[t1].push_back(e);
    54. e.next = t1;
    55. g[t2].push_back(e);
    56. }
    57. for (int i = 1; i <= m; i++) {
    58. priority_queueint, int>, vectorint, int>>, greaterint, int>>>empty;
    59. swap(empty, q);
    60. memset(dis[i], 127, sizeof(dis[i]));
    61. memset(vis, 0, sizeof(vis));
    62. dis[i][i + 1000] = 0;
    63. q.push(make_pair(0, i + 1000));
    64. while (!q.empty()) {
    65. int u = q.top().second;
    66. q.pop();
    67. if (vis[u]) {
    68. continue;
    69. }
    70. vis[u] = 1;
    71. for (int j = 0; j < g[u].size(); j++) {
    72. int v = g[u][j].next;
    73. if (dis[i][v] > dis[i][u] + g[u][j].dis) {
    74. dis[i][v] = dis[i][u] + g[u][j].dis;
    75. q.push(make_pair(dis[i][v], v));
    76. }
    77. }
    78. }
    79. }
    80. for (int i = 1; i <= m; i++) {
    81. a[i].indexx = i;
    82. a[i].flag = 0;
    83. a[i].avg = 0;
    84. a[i].minn = (1 << 30);
    85. for (int j = 1; j <= n; j++) {
    86. if (dis[i][j] > ds) {
    87. a[i].flag = 1;
    88. break;
    89. }
    90. a[i].avg += dis[i][j] * 1.0;
    91. if (dis[i][j] < a[i].minn) {
    92. a[i].minn = dis[i][j] * 1.0;
    93. }
    94. }
    95. a[i].avg /= (1.0 * n);
    96. if (a[i].avg - (((int)(a[i].avg * 10)) * 1.0 / 10.0 + 0.05) >= 0.0) {
    97. a[i].avg = ((int)(a[i].avg * 10)) * 1.0 / 10.0 + 0.1;
    98. }
    99. }
    100. sort(a + 1, a + 1 + m, cmp);
    101. for (int i = 1; i <= m; i++) {
    102. if (!a[i].flag) {
    103. cout << 'G' << a[i].indexx << endl;
    104. cout << setprecision(1) << fixed << a[i].minn << ' ' << setprecision(1) << fixed << a[i].avg;
    105. return 0;
    106. }
    107. }
    108. cout << "No Solution";
    109. return 0;
    110. }

  • 相关阅读:
    关于文件上传的前后端优化
    华为麒麟服务器--硬盘问题
    ArcGIS与Excel分区汇总统计三调各地类面积!数据透视表与汇总统计!
    OpenWrt 软路由介绍
    python+django+vue酒店入住客房管理系统
    星闪技术 NearLink 一种专门用于短距离数据传输的新型无线通信技术
    安装第三方包报错 error: Microsoft Visual C++ 14.0 or greater is required——解决办法
    Linux命令查看pcap包报文数量、包体包含内容、包长
    创建一个Keil项目
    大数据-Spark-Spark开发高频面试题
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126465972