垃圾箱分布

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住。所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的范围内。

现给定一个居民区的地图,以及若干垃圾箱的候选地点,请你推荐最合适的地点。如果解不唯一,则输出到所有居民点的平均距离最短的那个解。如果这样的解还是不唯一,则输出编号最小的地点。

输入格式:

输入第一行给出4个正整数:N(<= 103)是居民点的个数;M(<= 10)是垃圾箱候选地点的个数;K(<= 104)是居民点和垃圾箱候选地点之间的道路的条数;DS是居民点与垃圾箱之间不能超过的最大距离。所有的居民点从1到N编号,所有的垃圾箱候选地点从G1到GM编号。

随后K行,每行按下列格式描述一条道路:
P1 P2 Dist
其中P1和P2是道路两端点的编号,端点可以是居民点,也可以是垃圾箱候选点。Dist是道路的长度,是一个正整数。

输出格式:

首先在第一行输出最佳候选地点的编号。然后在第二行输出该地点到所有居民点的最小距离和平均距离。数字间以空格分隔,保留小数点后1位。如果解不存在,则输出“No Solution”。

输入样例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

输出样例1:

  1. G1
  2. 2.0 3.3

输入样例2:

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

输出样例2:

  1. No Solution

详见代码,dijstra

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <algorithm>
  5. #define inf 0x3f3f3f3f
  6. using namespace std;
  7. int n,k,m,ds;
  8. int e[][];
  9. bool v[];
  10. int d[];
  11. int kk=;
  12. struct node
  13. {
  14. int bh;
  15. double p;
  16. int h;
  17. int zd;
  18. }a[];
  19. bool cmp(node x,node y)
  20. {
  21. if(x.zd==y.zd)
  22. {
  23. if(x.p==y.p)
  24. return x.bh<y.bh;
  25. else return x.p<y.p;
  26. }
  27. else return x.zd>y.zd;//最短距离要最长
  28. }
  29. void dijstra(int s)
  30. {
  31. int i;
  32. memset(v,,sizeof(v));
  33. v[s]=;
  34. int zd=inf;
  35. for(i=;i<=n+m;i++)
  36. {
  37. d[i]=e[s][i];
  38. if(!v[i]&&i<=n&&d[i]<zd)//条件要写全
  39. zd=d[i];
  40. }
  41. while()
  42. {
  43. int k=-;
  44. int mm=inf;
  45. for(i=;i<=n+m;i++)
  46. {
  47. if(!v[i]&&d[i]<mm)
  48. {
  49. k=i;
  50. mm=d[i];
  51. }
  52. }
  53. if(k==-) break;
  54. v[k]=;
  55. for(i=;i<=n+m;i++)
  56. {
  57. if(d[i]>d[k]+e[k][i])
  58. {
  59. d[i]=d[k]+e[k][i];
  60. if(!v[i]&&i<=n&&d[i]<zd)
  61. zd=d[i];//把最短的距离找出来
  62. }
  63. }
  64. }
  65. int ss=;
  66. for(i=;i<=n;i++)
  67. {
  68. if(d[i]>ds) break;//最短路径超出范围的不要
  69. else ss+=d[i];
  70. }
  71. if(i==n+)
  72. {
  73. a[kk].bh =s-n;
  74. a[kk].h=ss;
  75. a[kk].zd=zd;
  76. a[kk++].p=ss*1.0/n;
  77. }
  78. }
  79.  
  80. int main()
  81. {
  82. cin>>n>>m>>k>>ds;
  83. int i,j;
  84. for(i=;i<=n+m;i++)
  85. for(j=;j<=n+m;j++)
  86. {
  87. if(i==j) e[i][j]=;
  88. else e[i][j]=inf;
  89. }
  90. for(i=;i<=k;i++)
  91. {
  92. string a,b;//G的处理
  93. int z;
  94. cin>>a>>b>>z;
  95. int x=,y=;
  96. if(a[]!='G')
  97. {
  98. int l=a.length ();
  99. for(int i=;i<l;i++)
  100. {
  101. x=x*+a[i]-'';
  102. }
  103. }
  104. else
  105. {
  106. int l=a.length ();
  107. for(int i=;i<l;i++)
  108. {
  109. x=x*+a[i]-'';
  110. }
  111. x+=n;
  112. }
  113.  
  114. if(b[]!='G')
  115. {
  116. int l=b.length ();
  117. for(int i=;i<l;i++)
  118. {
  119. y=y*+b[i]-'';
  120. }
  121. }
  122. else
  123. {
  124. int l=b.length ();
  125. for(int i=;i<l;i++)
  126. {
  127. y=y*+b[i]-'';
  128. }
  129. y+=n;
  130. }
  131. if(e[x][y]>z)
  132. {
  133. e[x][y]=z;
  134. e[y][x]=z;
  135. }
  136. }
  137. for(int i=n+;i<=m+n;i++)//遍历每一个垃圾箱
  138. {
  139. dijstra(i);
  140. }
  141. if(k==) cout<<"No Solution";
  142. else
  143. {
  144. sort(a+,a+kk,cmp);//排个序,是kk
  145. if(a[].bh==) cout<<"No Solution";
  146. else
  147. {
  148. cout<<"G"<<a[].bh<<endl;
  149. printf("%.1lf",a[].zd*1.0);cout<<" ";
  150. printf("%.1lf",a[].p);
  151. }
  152. }
  153.  
  154. return ;
  155. }

PAT 垃圾箱分布(30分)dijstra的更多相关文章

  1. 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 ...

  2. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  3. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  4. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  5. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  6. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  7. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  8. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

随机推荐

  1. Go CSP模型

    CSP 是 Communicating Sequential Process 的简称,中文可以叫做通信顺序进程,是一种并发编程模型,由 Tony Hoare 于 1977 年提出.简单来说,CSP 模 ...

  2. MapReduce-计数器

    计数器 计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计.计数器还可辅助诊断系统故障.根据计数器值来记录某一特定事件的发生比分析一堆日志文件容易得多.内置计数器Hadoop为每个作业维 ...

  3. UVA 10909 Lucky Number(树状数组+二分+YY)

    此题测试时预处理等了很久,结果470ms过了...... 题意:开始不怎么懂,结果发现是这个: 波兰裔美国数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)在20世纪50年代中期开发出了另一种 ...

  4. 分开统计的sql写法

    DECLARE @StartDate DATETIME= '2017-10-13 00:00:00';DECLARE @EndDate DATETIME= '2017-11-13 23:00:00'; ...

  5. Spring Boot 注释

    1.@RestController@RestController ≍ @Controller + @ResponseBody在Controller文件 public class xxxx 前面加用于返 ...

  6. pandas read_sql与read_sql_table、read_sql_query 的区别

    一:创建链接数据库引擎 from sqlalchemy import create_engine db_info = {'user':'user', 'password':'pwd', 'host': ...

  7. mysql数据库优化课程---2、命令其实也就是那几个单词

    mysql数据库优化课程---2.命令其实也就是那几个单词 一.总结 一句话总结: 比如show,use,alter 1.开启和关闭mysql服务? Windows下:net start/stop m ...

  8. spring boot: @Retention注解 @Documented 注解 @Inherited 注解

    http://www.jb51.net/article/55371.htm Retention注解 Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:1.Retenti ...

  9. selenium webdriver如何添加cookie

    一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies()                  获得cookie信息 add_c ...

  10. 打印控件Lodop

    官网:http://www.lodop.net/demo.html Lodop.C-Lodop使用说明及样例   Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又 ...