先求出最小生成树,然后枚举树上的边,对于每条边“分别”找出这条割边形成的两个块中点权最大的两个

1.因为结果是A/B。A的变化会引起B的变化,两个制约。无法直接贪心出最大的A/B。故要通过枚举

2.无论magic road要加在哪里。加的边是否是最小生成树上的边,都会产生环,我们都要选择一条边删掉

注意删掉的边必须是树的环上的边。为了使结果最大。即找出最大的边

3.能够枚举两点。找出边,也能够枚举边,找出点,我是用后者。感觉比較easy写也好理解

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cstring>
  7. #include <cmath>
  8. using namespace std;
  9. const int INF=(1<<31)-1;
  10. int num;
  11. bool visit[1111];
  12. struct city{int x,y,popu;}point[1111];
  13. vector<int>G[1111];
  14. double getdis(city& a,city& b)
  15. {
  16. double dx=a.x-b.x;
  17. double dy=a.y-b.y;
  18. return sqrt(dx*dx+dy*dy);
  19. }
  20. double dis[1111][1111];
  21. double prim()
  22. {
  23. double sum=0;
  24. double dist[1111];
  25. fill(dist,dist+1111,INF*1.0);
  26. double minedge=INF;
  27. int now=1,min_p;
  28. int pre[1111];
  29. memset(pre,0,sizeof(pre));
  30. dist[now]=0;
  31. visit[1]=true;
  32. for(int t=1;t<num;t++)
  33. {
  34. for(int i=1;i<=num;i++)
  35. {
  36. if(i!=now && !visit[i] &&dist[i]>dis[now][i])
  37. {
  38. pre[i]=now;
  39. dist[i]=dis[now][i];
  40. }
  41. }
  42. minedge=INF;
  43. for(int i=1;i<=num;i++)
  44. {
  45. if(!visit[i]&&minedge>dist[i])
  46. {
  47. minedge=dist[i];
  48. min_p=i;
  49. }
  50. }
  51. G[pre[min_p]].push_back(min_p);
  52. G[min_p].push_back(pre[min_p]);
  53. sum+=dis[min_p][pre[min_p]];
  54. now=min_p;
  55. visit[now]=true;
  56. }
  57. return sum;
  58. }
  59. int dfs(int v,int fa)
  60. {
  61. int maxn=point[v].popu;
  62. for(int j=0;j<G[v].size();j++)
  63. {
  64. if(G[v][j]!=fa)
  65. {
  66. maxn=max(maxn,dfs(G[v][j],v));
  67. }
  68. }
  69. return maxn;
  70. }
  71. int main()
  72. {
  73. #ifndef ONLINE_JUDGE
  74. freopen("G:/1.txt","r",stdin);
  75. freopen("G:/2.txt","w",stdout);
  76. #endif
  77. int T;
  78. scanf("%d",&T);
  79. while(T--)
  80. {
  81. scanf("%d",&num);
  82. for(int i=1;i<=num;i++)
  83. {
  84. scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].popu);
  85. }
  86. for(int i=1;i<=num;i++)
  87. {
  88. for(int j=1;j<=num;j++)
  89. {
  90. dis[i][j]=getdis(point[i],point[j]);
  91. }
  92. }
  93. double sum=prim();
  94. double ans=0;
  95. for(int i=1;i<=num;i++)
  96. {
  97. for(int j=0;j<G[i].size();j++)
  98. {
  99. int t1=dfs(i,G[i][j]);
  100. int t2=dfs(G[i][j],i);
  101. ans=max(ans,(t1+t2)*1.0/(sum-dis[i][G[i][j]]));
  102. }
  103. }
  104. //ans+=(1e-8);
  105. printf("%.2f\n",ans);
  106. for(int i=1;i<=num;i++)
  107. {
  108. G[i].clear();
  109. }
  110. memset(point,0,sizeof(city)*(num+1));
  111. memset(visit,0,sizeof(bool)*(num+1));
  112. }
  113. }

HDU4081 Qin Shi Huang&#39;s National Road System【prim最小生成树+枚举】的更多相关文章

  1. HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

    点击打开链接题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  2. HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

    题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大.全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i ...

  3. HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形

    题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...

  4. HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏

    Qin Shi Huang's National Road System                                                                 ...

  5. HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)

    题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: ...

  7. HDU4081 Qin Shi Huang's National Road System(次小生成树)

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

  8. hdu4081 Qin Shi Huang's National Road System 次小生成树

    先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...

  9. HDU4081 Qin Shi Huang's National Road System

    先求最小生成树 再遍历每一对顶点,如果该顶点之间的边属于最小生成树,则剪掉这对顶点在最小生成树里的最长路径 否则直接剪掉连接这对顶点的边~ 用prim算法求最小生成树最长路径的模板~ #include ...

随机推荐

  1. Python-搭建Nginx+Django环境

    1.安装 flup 模块 下载:http://projects.unbit.it/downloads/uwsgi-latest.tar.gz 安装:python setup.py install 2. ...

  2. Cordova 快速入门记录

    本篇文章由:http://xinpure.com/cordova-quick-start-recording/ 记一笔 Cordova 官网入门文档 Get Started Fast,言简意该.通俗易 ...

  3. mysql索引二

    理解MySQL——索引与优化 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优 的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储1 ...

  4. php连接sql2005

    连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替换. 下载正确版本的 ntwdblib.dll (2000.80.194.0),地址: http ...

  5. Python 列表 list() 方法

    描述 Python 列表 list() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为列表. 注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中. ...

  6. 日期常用操作类DateUtil

    一.给定yyyy-MM-dd hh:mm:ss格式的字符串,返回Date. public Date convertStr2Date(String dateString) { try { SimpleD ...

  7. opensips编译安装时可能遇到的问题

    错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...

  8. c#(winform)中自定义ListItem类方便ComboBox添加Item项

    1.定义ListItem类 public class ListItem { private string _key = string.Empty; private string _value = st ...

  9. Node Redis 小试

    Redis 是一个高性能的 key-value 数据库,为了保证效率,数据都是缓存在内存中,在执行频繁而又复杂的数据库查询条件时,可以使用 Redis 缓存一份查询结果,以提升应用性能. 背景 如果一 ...

  10. 多国语言解决方案gnu.gettext + poedit

    1.工具简介 1.1.关于i18n i18n其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数是“国际化”的简称. i10n为资源本地化,全称为Locali ...