Qin Shi Huang's National Road System

HDU - 4081

感觉这道题和hdu4756很像...

求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以(最小生成树-E1)的最大值

其中(最小生成树-E1)必须是最小的

先跑一遍prim 跑完之后在最小生成树里面dp

dp[i][j] = i到j的路径中最大的那条边 最小生成树减去dp[i][j]肯定会最小

代码如下

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. const double inf = 0x3f3f3f3f3f;
  8. const int maxn = ;
  9. struct Point {
  10. double x, y;
  11. int n;
  12. } point[maxn];
  13. struct Edge {
  14. int to;
  15. int next;
  16. } edge[maxn<<];
  17. int n, cnt, head[maxn], pre[maxn];
  18. double dis[maxn][maxn], lowc[maxn], sum, dp[maxn][maxn];
  19. bool vis[maxn];
  20. inline void addedge(int u, int v) {
  21. edge[cnt].to = v;
  22. edge[cnt].next = head[u];
  23. head[u] = cnt++;
  24. }
  25. inline double Distance(const Point& lhs, const Point& rhs) {
  26. return sqrt((lhs.x - rhs.x) * (lhs.x - rhs.x) + (lhs.y - rhs.y) * (lhs.y - rhs.y));
  27. }
  28. void prim() {
  29. sum = 0.0;
  30. memset(vis, , sizeof(vis));
  31. memset(pre, , sizeof(pre));
  32. for (int i = ; i < n; i++) lowc[i] = dis[][i];
  33. vis[] = true;
  34. for (int i = ; i < n; i++) {
  35. double minc = inf;
  36. int p = -;
  37. for (int j = ; j < n; j++) {
  38. if (!vis[j] && minc > lowc[j]) {
  39. minc = lowc[j];
  40. p = j;
  41. }
  42. }
  43. sum += minc;
  44. vis[p] = true;
  45. addedge(p, pre[p]);
  46. addedge(pre[p], p);
  47. for (int j = ; j < n; j++) {
  48. if (!vis[j] && lowc[j] > dis[p][j]) {
  49. lowc[j] = dis[p][j];
  50. pre[j] = p;
  51. }
  52. }
  53. }
  54. }
  55. void dfs(int u, int root) {
  56. vis[u] = true;
  57. for (int i = head[u]; ~i; i = edge[i].next) {
  58. int v = edge[i].to;
  59. if (!vis[v]) {
  60. dp[root][v] = max(dp[root][u], dis[u][v]);
  61. dfs(v, root);
  62. }
  63. }
  64. }
  65. int main() {
  66. int T;
  67. scanf("%d", &T);
  68. while (T--) {
  69. scanf("%d", &n);
  70. for (int i = ; i < n; i++) {
  71. scanf("%lf%lf%d", &point[i].x, &point[i].y, &point[i].n);
  72. }
  73. for (int i = ; i < n; i++) {
  74. for (int j = i + ; j < n; j++) {
  75. dis[i][j] = dis[j][i] = Distance(point[i], point[j]);
  76. }
  77. }
  78. memset(head, -, sizeof(head));
  79. cnt = ;
  80. prim();
  81. for (int i = ; i < n; i++) {
  82. memset(vis, ,sizeof(vis));
  83. dfs(i, i);
  84. }
  85. double ans = ;
  86. for (int i = ; i < n; i++) {
  87. for (int j = i + ; j < n; j++) {
  88. double temp = sum - dp[i][j];
  89. temp = (point[i].n + point[j].n) / temp;
  90. ans = max(ans, temp);
  91. }
  92. }
  93. printf("%.2f\n", ans);
  94. }
  95. return ;
  96. }

Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)的更多相关文章

  1. LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST

    LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. [hdu P4081] Qin Shi Huang’s National Road System

    [hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  3. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  4. hdu 4081 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 ...

  5. hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)

    题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...

  6. HDU 4081 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 ...

  7. HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】

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

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

  9. UValive 5713 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 ...

随机推荐

  1. 前端入门18-JavaScript进阶之作用域链

    声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...

  2. vue2.x 下载后台传过来的流文件(excel)后乱码问题

    1.接口返回的流和头部: 2.下载流文件的代码 方法一:是用了插件 https://github.com/kennethjiang/js-file-download 方法二:是用了 blob 不管哪种 ...

  3. Bootstrap 实战之响应式个人博客 (二)

    阅读本博文前请参考:Bootstrap 实战之响应式个人博客 (一) 一.博客 1.结构 整体博客详情页的结构共包括四部分: 导航栏 博客主体内容 右侧栏:全局搜索框,广告位,推荐阅读 页尾 其中导航 ...

  4. android找不到aar包

    转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/9939663.html  在做项目的时候引入aar包,编译的时候却提示错误(这个错误大概说的是...... ...

  5. onScrollChanged()

    转载请标明出处:http://www.cnblogs.com/tangZH/p/8428100.html  onScrollChanged里面有四个参数 @Overrideprotected void ...

  6. Andriod Studio安装教程

    最近开设安卓课程,无奈于开发团队不再更新eclipse上sdk兼容问题,在eclipse上浪费了两天时间,换了Andriod Studio, Andriod Studio下载网址:http://www ...

  7. ASP.NET中弹出消息框的几种常见方法

    在ASP.NET网站开发中,经常需要使用到alert消息框,尤其是在提交网页的时候,往往需要在服务器端对数据进行检验,并给出提示或警告. 这里,仅介绍几种不同的实现方法. 1.众所周知的方法是采用如下 ...

  8. Linux LVM学习总结——Insufficient Free Extents for a Logical Volume

    如下所示,在创建LV的时候,偶尔会遇到"Volume group "xxxx" has insufficient free space (xxxx extents): x ...

  9. 【原】Java学习笔记024 - 包装类

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 之前对于基本数据类 ...

  10. debian 9 开机启动

    由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local 文件,但是 Debian 9 默认不带 /etc/rc.local 文件,而 rc. ...