枚举一下选的位置,每次算一下就可以了。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<stack>
  7. #include<queue>
  8. #include<string>
  9. #include<algorithm>
  10. using namespace std;
  11.  
  12. const int INF=0x7FFFFFFF;
  13. const int maxn=+;
  14. int n,m,k,ds;
  15. struct Edge
  16. {
  17. int u,v;
  18. int dis;
  19. }e[*maxn];
  20. vector<int>g[maxn];
  21. int tot;
  22. int flag[maxn],dis[maxn];
  23. double ans1,ans2;
  24. int ans;
  25. int MIN;
  26.  
  27. int get(char *s)
  28. {
  29. int num=;
  30. if(s[]!='G')
  31. {
  32. for(int i=;s[i];i++)
  33. num=num*+s[i]-'';
  34. }
  35. else
  36. {
  37. for(int i=;s[i];i++)
  38. num=num*+s[i]-'';
  39. num=num+n;
  40. }
  41. return num;
  42. }
  43.  
  44. void SPFA(int s)
  45. {
  46. queue<int>Q;
  47. memset(flag,,sizeof flag);
  48. for(int i=;i<=n+m;i++) dis[i]=INF;
  49. Q.push(s); flag[s]=; dis[s]=;
  50. while(!Q.empty())
  51. {
  52. int head=Q.front(); Q.pop(); flag[head]=;
  53. for(int i=;i<g[head].size();i++)
  54. {
  55. int id=g[head][i];
  56. if(dis[head]+e[id].dis<dis[e[id].v])
  57. {
  58. dis[e[id].v]=dis[head]+e[id].dis;
  59. if(flag[e[id].v]==)
  60. {
  61. flag[e[id].v]=;
  62. Q.push(e[id].v);
  63. }
  64. }
  65. }
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71. scanf("%d%d%d%d",&n,&m,&k,&ds);
  72.  
  73. tot=;
  74. for(int i=;i<=k;i++)
  75. {
  76. char u[],v[]; int dis;
  77. scanf("%s%s%d",u,v,&dis);
  78. e[tot].u=get(u),e[tot].v=get(v);
  79. e[tot].dis=dis;
  80. g[get(u)].push_back(tot),tot++;
  81.  
  82. e[tot].u=get(v),e[tot].v=get(u);
  83. e[tot].dis=dis;
  84. g[get(v)].push_back(tot),tot++;
  85. }
  86.  
  87. ans1=1.0*INF;
  88. ans2=1.0*INF;
  89. MIN=;
  90.  
  91. for(int i=n+;i<=n+m;i++)
  92. {
  93. SPFA(i);
  94. int sum=,MIN_NOW=INF;
  95. for(int j=;j<=n;j++)
  96. {
  97. if(dis[j]>ds) {sum=-;break;}
  98. else
  99. {
  100. sum=sum+dis[j];
  101. MIN_NOW=min(MIN_NOW,dis[j]);
  102. }
  103. }
  104. if(sum==-) continue;
  105.  
  106. if(MIN_NOW>MIN)
  107. {
  108. MIN=MIN_NOW;
  109. ans2=1.0*sum/n;
  110. ans1=1.0*MIN;
  111. ans=i;
  112. }
  113.  
  114. else if(MIN_NOW==MIN&&1.0*sum/n<ans2)
  115. {
  116. ans2=1.0*sum/n;
  117. ans=i;
  118. }
  119. }
  120. if(MIN==) printf("No Solution\n");
  121. else
  122. {
  123. printf("G%d\n",ans-n);
  124. printf("%.1lf %.1lf\n",ans1,ans2);
  125. }
  126. return ;
  127. }

PAT (Advanced Level) 1072. Gas Station (30)的更多相关文章

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

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

  2. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  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)

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

  8. 1072. Gas Station (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)(30 分)

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

随机推荐

  1. HDU2673:shǎ崽 OrOrOrOrz

    Problem Description Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Inter ...

  2. sublime Text 常用操作

    原文出处:http://www.php100.com/html/it/focus/2014/1030/7666.html 1. 多光标操作:只要按下Cmd(Windows系统下Ctrl)键,再用鼠标选 ...

  3. iOS打包app发给测试人员测试

    说明:在项目开发过程中经常需要开发人员将项目打包成ipa包后,发给测试人员进行测试.本文贴图对打包的过程简单介绍. 一.Product ->archive (注意,不能是模拟器状态,如果当前调试 ...

  4. 自定义AccessDeniedHandler

    在Spring默认的AccessDeniedHandler中只有对页面请求的处理,而没有对Ajax的处理.而在项目开发是Ajax又是我们要常用的技术,所以我们可以通过自定义AccessDeniedHa ...

  5. Head First--设计模式(装饰者模式)

    从现在开始,我将以写一个小软件的方式来进行模式识别的学习,包含需求分析,功能模块设计,原理图设计,程序实现这个几个部分,可能具体的模式模块不可能都包括在内. 需求分析:星巴兹咖啡店已经遍布全世界的很多 ...

  6. bind启动时提示953端口被使用

    部署DNS的时候遇到个奇葩的问题,总是提示 couldn't add command channel 0.0.0.0#953: address in use 实际上系统上并没有进程使用953端口.查询 ...

  7. hostent h_addr_list

    struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias ...

  8. nginx 正则表达式

    1.nginx配置基础 1.正则表达式匹配 ~ 区分大小写匹配 ~* 不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 ^ 以什么开头的匹配 $ 以什么结尾的匹配 转义字符.可 ...

  9. c#抓取网页内容乱码的解决方案

    写过爬虫的同学都知道,这是个很常见的问题了,一般处理思路是: 使用HttpWebRequest发送请求,HttpWebResponse来接收,判断HttpWebResponse中”Content-Ty ...

  10. Win7 “Bluetooth设置”对话框无法打开,及无法查找到设备

    方法是在百度上找到的,试用成功. 1.打开开始菜单中的运行选项,然后在对话框中输入services.msc,回车打开服务界面: 2.然后在弹出来的服务窗口中查找到Bluetooth Support S ...