题目大意:给你n个点然后让你求出去掉一条边之后所形成的最小生成树。

比較基础的次小生成树吧。

。。先prime一遍求出最小生成树。在dfs求出次小生成树。

Install Air Conditioning

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1038    Accepted Submission(s): 240

Problem Description



  NJUST carries on the tradition of HaJunGong. NJUST, who keeps up the ”people-oriented, harmonious development” of the educational philosophy and develops the ”unity, dedication, truth-seeking, innovation” school motto, has now become an engineering-based,
multidisciplinary university.



  As we all know, Nanjing is one of the four hottest cities in China. Students in NJUST find it hard to fall asleep during hot summer every year. They will never, however, suffer from that hot this year, which makes them really excited. NJUST’s 60th birthday
is approaching, in the meantime, 50 million is spent to install air conditioning among students dormitories. Due to NJUST’s long history, the old circuits are not capable to carry heavy load, so it is necessary to set new high-load wires. To reduce cost, every
wire between two dormitory is considered a segment. Now, known about all the location of dormitories and a power plant, and the cost of high-load wire per meter, Tom200 wants to know in advance, under the premise of all dormitories being able to supply electricity,
the minimum cost be spent on high-load wires. And this is the minimum strategy. But Tom200 is informed that there are so many wires between two specific dormitories that we cannot set a new high-load wire between these two, otherwise it may have potential
risks. The problem is that Tom200 doesn’t know exactly which two dormitories until the setting process is started. So according to the minimum strategy described above, how much cost at most you'll spend?

 
Input
  The first line of the input contains a single integer T(T ≤ 100), the number of test cases.

  For each case, the first line contains two integers n(3 ≤ n ≤ 1000), k(1 ≤ k ≤ 100). n represents n-1 dormitories and one power plant, k represents the cost of high-load wire per meter. n lines followed contains two integers x, y(0 ≤ x, y ≤ 10000000), representing
the location of dormitory or power plant. Assume no two locations are the same, and no three locations are on a straight line. The first one is always the location of the power plant.
 
Output
  For each case, output the cost, correct to two decimal places.
 
Sample Input
  1. 2
  2. 4 2
  3. 0 0
  4. 1 1
  5. 2 0
  6. 3 1
  7. 4 3
  8. 0 0
  9. 1 1
  10. 1 0
  11. 0 1
 
Sample Output
  1. 9.66
  2. 9.00
  1. #include <set>
  2. #include <map>
  3. #include <queue>
  4. #include <math.h>
  5. #include <vector>
  6. #include <string>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <iostream>
  11. #include <algorithm>
  12.  
  13. #define LL __int64
  14. using namespace std;
  15.  
  16. const int INF = 0x3f3f3f3f;
  17. const int maxn = 1010;
  18. const int N = 1010;
  19. const int M = 300010;
  20. struct node
  21. {
  22. int x, y;
  23. } p[maxn];
  24.  
  25. struct node1
  26. {
  27. int to, next;
  28. } f[M];
  29.  
  30. int pre[N], head[N], flag[N];
  31. int vis[N][N];
  32. double low[N];
  33. double dis[N][N], dp[N][N];
  34. int n, m, num;
  35. double sum;
  36.  
  37. void init()
  38. {
  39. sum = 0;
  40. num = 0;
  41. memset(flag, 0, sizeof(flag));
  42. memset(vis, 0, sizeof(vis));
  43. memset(head, -1, sizeof(head));
  44. memset(dp, 0, sizeof(dp));
  45. }
  46.  
  47. double Dis(int x0, int y0, int x1, int y1)
  48. {
  49. return sqrt(1.0*(x0-x1)*(x0-x1) + 1.0*(y0-y1)*(y0-y1));
  50. }
  51.  
  52. void add(int s, int t)
  53. {
  54. f[num].to = t;
  55. f[num].next = head[s];
  56. head[s] = num ++;
  57. }
  58.  
  59. void prime()
  60. {
  61. for(int i = 1; i <= n; i++)
  62. {
  63. low[i] = dis[1][i];
  64. pre[i] = 1;
  65. }
  66. flag[1] = 1;
  67. for(int i = 1; i < n; i++)
  68. {
  69. double Min = INF;
  70. int v;
  71. for(int j = 1; j <= n; j++)
  72. {
  73. if(!flag[j] && Min > low[j])
  74. {
  75. v = j;
  76. Min = low[j];
  77. }
  78. }
  79. sum += Min;
  80. vis[pre[v]][v] = vis[v][pre[v]] = 1;
  81. add(v, pre[v]);
  82. add(pre[v], v);
  83. flag[v] = 1;
  84. for(int j = 1; j <= n; j++)
  85. {
  86. if(!flag[j] && low[j] > dis[v][j])
  87. {
  88. low[j] = dis[v][j];
  89. pre[j] = v;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. double dfs(int cur, int u, int fa) //用cur更新cur点所在的子树和另外子树的最短距离
  96. {
  97. double ans = INF;
  98. for(int i = head[u]; ~i; i = f[i].next) //沿着生成树的边遍历
  99. {
  100. if(f[i].to == fa)
  101. continue;
  102. double tmp = dfs(cur, f[i].to, u); //用cur更新的以当前边为割边的两个子树最短距离
  103. ans = min(tmp, ans); //以(fa,u)为割边的2个子树的最短距离
  104. dp[u][f[i].to] = dp[f[i].to][u] = min(tmp, dp[u][f[i].to]);
  105. }
  106. if(cur != fa) //生成树边不更新
  107. ans = min(ans, dis[cur][u]);
  108. return ans;
  109. }
  110.  
  111. int main()
  112. {
  113. int T;
  114. scanf("%d",&T);
  115. while(T--)
  116. {
  117. init();
  118. scanf("%d %d",&n, &m);
  119. for(int i = 1; i <= n; i++) scanf("%d %d",&p[i].x, &p[i].y);
  120. for(int i = 1; i <= n; i++)
  121. {
  122. for(int j = 1; j <= i; j++)
  123. {
  124. dp[i][j] = dp[j][i] = INF;
  125. if(i == j)
  126. {
  127. dis[i][j] = 0.0;
  128. continue;
  129. }
  130. dis[i][j] = dis[j][i] = Dis(p[i].x, p[i].y, p[j].x, p[j].y);
  131. }
  132. }
  133. prime();
  134. double ans = sum;
  135. for(int i = 0; i < n; i++) dfs(i, i, -1);
  136. for(int i = 2; i <= n; i++)
  137. {
  138. for(int j = 2; j < i; j++)
  139. {
  140. if(!vis[i][j]) continue;
  141. ans = max(ans, sum-dis[i][j]+dp[i][j]);
  142. }
  143. }
  144. printf("%.2lf\n",ans*m);
  145. }
  146. return 0;
  147. }

HDU 4756 Install Air Conditioning(次小生成树)的更多相关文章

  1. hdu 4756 Install Air Conditioning

    非正规做法,一个一个的暴,减一下枝,还得采用sort,qsort居然过不了…… #include <cstdio> #include <cmath> #include < ...

  2. HDU 4756 Install Air Conditioning (MST+树形DP)

    题意:n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个位置连通的最小花费的最大值. 析:因为要连通,还要权值最小,所以就是MST了,然后就是改变一条边,然后去找出改变 ...

  3. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

  4. hdu4756 Install Air Conditioning(MST + 树形DP)

    题目请戳这里 题目大意:给n个点,现在要使这n个点连通,并且要求代价最小.现在有2个点之间不能直接连通(除了第一个点),求最小代价. 题目分析:跟这题一样样的,唉,又是原题..先求mst,然后枚举边, ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

  6. HDU 4081Qin Shi Huang's National Road System(次小生成树)

    题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...

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

  8. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

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

随机推荐

  1. Linux 网络属性管理

    Linux网络基础管理-1:IPv4 地址分类:  点分十进制:0.0.0.0-255.255.255.255  A类: 0 0000000 - 0 1111111: 1-127 网络数:126, 1 ...

  2. linux下为firfox安装flash player

    1.去官网下载×.tar.gz包,如:flash_player_npapi_linux.x86_64.tar.gz 2.解压 tar -zxvf flash_player_npapi_linux.x8 ...

  3. Hadoop2.9.1安装教程_环境Ubuntu_VMware安装

    一,环境选择 Hadoop需要运行在linux系统之下,所以有以下两种选择:1,安装双系统,缺点:此方式比较麻烦而且并不适合初学者,因为之后的安装以及配置过程可能会遇到许多问题,这需要我们上网去搜索. ...

  4. Git:与eclipse搭配使用

    Git:与eclipse搭配使用 1)工程初始化为本地库 工程 ——>右键 ——>Team ——Share Project 在该目录下创建了本地库 这里可以设置用户签名 2)Eclipse ...

  5. 接口测试及接口Jmeter工具介绍

    一.接口类型及数据传递的格式 接口类型: 1.HTTP接口:通过GET或POST来获取数据,在数据处理上效率比较高 2.WebServer接口:通过SOAP协议来获取数据,比起http来说处理更加复杂 ...

  6. java分页之假分页

    假分页,顾名思义,不是真正的在数据库里进行过滤,而是从数据库查询之后,取得全部结果,在展现的时候做些手脚. import java.util.ArrayList; import java.util.L ...

  7. django models.py增加后MySQL数据库中并没有生成相应的表

    根据教程到添加并保存quest的时候报错了 1.models.py里面的命名没有错 2.查看mysite->settiongs下的INSTALLED_APPS设置正确 3.使用python ma ...

  8. IBM AppScan官方帮助文档错别字缺陷,IBM的測试人员也太粗心了吧

    袁术=元素?

  9. 37、ifconfig命令

    很多windows很熟悉ipconfig命令行工具.它被用来获取网络接口配置信息并对此进行改动.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config). 通常须 ...

  10. 4.git "Could not read from remote repository.Please make sure you have the correct access rights."解决方案

    转自:https://zhiku8.com/git-could-not-read-from-remote-repository.html 我们在使用git clone 或其他命令的时候,有时候会遇到这 ...