题目信息

1072. Gas Station (30)

时间限制200 ms

内存限制65536 kB

代码长度限制16000 B

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 (<= 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 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+1 到 n + m之中,依次对每一个网站dijkstra求最短路,同一时候维护全局最优值就可以

AC代码

  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <map>
  5. using namespace std;
  6. int n, m, k, ds, a, b, d;
  7. map<int, map<int, int> > mp;
  8. int maxLeng = -1, sum = 0x7fffffff, resId = -1;
  9. void dijkstra(int id)
  10. {
  11. vector<int> v(n + m + 1, 0x7fffffff); //dijkstra中记录起点到各点的最短距离
  12. vector<bool> vis(n + m + 1, false); //是否訪问
  13. v[id] = 0;
  14. int cnt = n + m; //须要訪问的点个数
  15. while (cnt--){
  16. int mx = 0x7fffffff, t = 0;
  17. for (int i = 1; i <= n + m; ++i){
  18. if (!vis[i] && v[i] <= mx){
  19. mx = v[i];
  20. t = i;
  21. }
  22. }
  23. if (t <= n && v[t] > ds) break; //超过最大辐射范围退出。一定注意此处的t <= n
  24. vis[t] = true;
  25. for (map<int, int>::iterator it = mp[t].begin(); it != mp[t].end(); ++it){
  26. v[it->first] = min(v[it->first], v[t] + it->second);
  27. }
  28. }
  29. if (cnt < 0){ //都在辐射范围内
  30. int minValue = *min_element(v.begin() + 1, v.begin() + n + 1); //最小值
  31. if (minValue > maxLeng){ //比全局最小值小则更新
  32. maxLeng = minValue;
  33. sum = accumulate(v.begin() + 1, v.begin() + n + 1, 0);
  34. resId = id;
  35. }else if (minValue == maxLeng){
  36. int t = accumulate(v.begin() + 1, v.begin() + n + 1, 0);
  37. if (t < sum){ //最小值与全局同样时若和更小则更新
  38. resId = id;
  39. sum = t;
  40. }
  41. }
  42. }
  43. }
  44. int main()
  45. {
  46. scanf("%d%d%d%d", &n, &m, &k, &ds);
  47. char s1[15], s2[15];
  48. for (int i = 0; i < k; ++i){
  49. scanf("%s%s%d", s1, s2, &d);
  50. sscanf((s1[0] == 'G') ? s1+1:s1, "%d", &a);
  51. sscanf((s2[0] == 'G') ?
  52. s2+1:s2, "%d", &b);
  53. a += (s1[0] == 'G') ? n : 0;
  54. b += (s2[0] == 'G') ?
  55. n : 0;
  56. mp[a][b] = mp[b][a] = d;
  57. }
  58. for (int i = 1; i <= m; ++i){
  59. dijkstra(n + i);
  60. }
  61. if (resId == -1){
  62. printf("No Solution\n");
  63. }else{
  64. printf("G%d\n%0.1f %0.1f\n", resId - n, 1.0 * maxLeng, 1.0 * sum / n);
  65. }
  66. return 0;
  67. }

个人游戏推广:

《10云方》与方块来次消除大战!

1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise的更多相关文章

  1. 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)

    题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...

  2. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  3. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  4. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  5. PAT Advanced 1072 Gas Station (30) [Dijkstra算法]

    题目 A gas station has to be built at such a location that the minimum distance between the station an ...

  6. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  7. 1072. Gas Station (30) 多源最短路

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  9. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

随机推荐

  1. Python3.0 操作MySQL数据库执行SQL语句

    py3不支持MySQLdb,需要导入pymysql模块 # coding: utf-8 # Team : Quality Management Center # Author:Carson # Dat ...

  2. vim里面搜索字符串

    直接在命令模式/+字符串就能搜索到,查找下一个,按“n”

  3. ibdata过大删除的方法

    1.做数据库的逻辑备份 mysqldump -uroot -p123456 -B xx xx xx xx > /backup/all.sql 2.停止mysql进程 service mysqld ...

  4. 零基础入门学习Python(23)--递归:这帮小兔崽子

    知识点 我们都知道兔子繁殖能力是惊人的,如下图: 我们可以用数学函数来定义: 假设我们需要求出经历了20个月后,总共有多少对小兔崽子? 迭代实现 def fab(n): n1 = 1 n2 = 1 n ...

  5. c++基础_字母图形

    #include <iostream> #include <algorithm> using namespace std; int main(){ ,m=,c; cin> ...

  6. ORM之连表操作

    ORM之连表操作 -----------------------------连表的正向操作------------------------- 在models.py中创建两张表UserType和User ...

  7. Ubuntu系统搭建django+nginx+uwsgi

    1. 在开发机上的准备工作 2. 在服务器上的准备工作 3.安装uwsgi 4.编写uwsgi配置文件,使用配置文件启动uwsgi 5. 安装nginx 6. 收集静态文件 7. 编写nginx配置文 ...

  8. Html、Css、JavaScript 遇到的问题总结

    $('body').scrollTop()无效得解决方案 鼠标滑轮获取到得值为0:var scrollTop = $('body').scrollTop(); 在页面中加一个随着页面滚动条滚动的小图片 ...

  9. 90-Standard Deviation 标准离差指标.(2015.7.4)

    Standard Deviation 标准离差指标 ~计算: StdDev = SQRT (SUM (CLOSE - SMA (CLOSE, N), N)^2)/N 注解: SQRT - 正方体根: ...

  10. noi.ac NOIP2018 全国热身赛 第二场 T3 color

    [题解] 我们可以发现每次修改之后叶子结点到根的路径最多分为两段:一段白色或者黑色,上面接另一段灰色的.二分+倍增找到分界点,然后更新答案即可. check的时候只需要判断当前节点对应的叶子结点的区间 ...