HDU 4756 Install Air Conditioning(次小生成树)
题目大意:给你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
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?
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.
2 4 2
0 0
1 1
2 0
3 1 4 3
0 0
1 1
1 0
0 1
9.66
9.00
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define LL __int64
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = 1010;
const int N = 1010;
const int M = 300010;
struct node
{
int x, y;
} p[maxn]; struct node1
{
int to, next;
} f[M]; int pre[N], head[N], flag[N];
int vis[N][N];
double low[N];
double dis[N][N], dp[N][N];
int n, m, num;
double sum; void init()
{
sum = 0;
num = 0;
memset(flag, 0, sizeof(flag));
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
memset(dp, 0, sizeof(dp));
} double Dis(int x0, int y0, int x1, int y1)
{
return sqrt(1.0*(x0-x1)*(x0-x1) + 1.0*(y0-y1)*(y0-y1));
} void add(int s, int t)
{
f[num].to = t;
f[num].next = head[s];
head[s] = num ++;
} void prime()
{
for(int i = 1; i <= n; i++)
{
low[i] = dis[1][i];
pre[i] = 1;
}
flag[1] = 1;
for(int i = 1; i < n; i++)
{
double Min = INF;
int v;
for(int j = 1; j <= n; j++)
{
if(!flag[j] && Min > low[j])
{
v = j;
Min = low[j];
}
}
sum += Min;
vis[pre[v]][v] = vis[v][pre[v]] = 1;
add(v, pre[v]);
add(pre[v], v);
flag[v] = 1;
for(int j = 1; j <= n; j++)
{
if(!flag[j] && low[j] > dis[v][j])
{
low[j] = dis[v][j];
pre[j] = v;
}
}
}
} double dfs(int cur, int u, int fa) //用cur更新cur点所在的子树和另外子树的最短距离
{
double ans = INF;
for(int i = head[u]; ~i; i = f[i].next) //沿着生成树的边遍历
{
if(f[i].to == fa)
continue;
double tmp = dfs(cur, f[i].to, u); //用cur更新的以当前边为割边的两个子树最短距离
ans = min(tmp, ans); //以(fa,u)为割边的2个子树的最短距离
dp[u][f[i].to] = dp[f[i].to][u] = min(tmp, dp[u][f[i].to]);
}
if(cur != fa) //生成树边不更新
ans = min(ans, dis[cur][u]);
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n, &m);
for(int i = 1; i <= n; i++) scanf("%d %d",&p[i].x, &p[i].y);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
dp[i][j] = dp[j][i] = INF;
if(i == j)
{
dis[i][j] = 0.0;
continue;
}
dis[i][j] = dis[j][i] = Dis(p[i].x, p[i].y, p[j].x, p[j].y);
}
}
prime();
double ans = sum;
for(int i = 0; i < n; i++) dfs(i, i, -1);
for(int i = 2; i <= n; i++)
{
for(int j = 2; j < i; j++)
{
if(!vis[i][j]) continue;
ans = max(ans, sum-dis[i][j]+dp[i][j]);
}
}
printf("%.2lf\n",ans*m);
}
return 0;
}
HDU 4756 Install Air Conditioning(次小生成树)的更多相关文章
- hdu 4756 Install Air Conditioning
非正规做法,一个一个的暴,减一下枝,还得采用sort,qsort居然过不了…… #include <cstdio> #include <cmath> #include < ...
- HDU 4756 Install Air Conditioning (MST+树形DP)
题意:n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个位置连通的最小花费的最大值. 析:因为要连通,还要权值最小,所以就是MST了,然后就是改变一条边,然后去找出改变 ...
- Install Air Conditioning HDU - 4756(最小生成树+树形dp)
Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...
- hdu4756 Install Air Conditioning(MST + 树形DP)
题目请戳这里 题目大意:给n个点,现在要使这n个点连通,并且要求代价最小.现在有2个点之间不能直接连通(除了第一个点),求最小代价. 题目分析:跟这题一样样的,唉,又是原题..先求mst,然后枚举边, ...
- hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
- HDU 4081Qin Shi Huang's National Road System(次小生成树)
题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...
- 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 ...
- HDU 4081 Qin Shi Huang's National Road System [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
- 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 ...
随机推荐
- Swoole WebSoctet 使用 zlib 压缩之 PHP 与 pako.js
一些理论知识 先说一下deflate算法吧,deflate是zip压缩文件的默认算法, 其实deflate现在不光用在zip文件中, 在7z, xz等其他的压缩文件中都用, 实际上deflate只是一 ...
- 排序代码(python,c++) 及 基本算法复杂度
0.导语 本节为手撕代码系列之第一弹,主要来手撕排序算法,主要包括以下几大排序算法: 直接插入排序 冒泡排序 选择排序 快速排序 希尔排序 堆排序 归并排序 1.直接插入排序 [算法思想] 每一步将一 ...
- 每天学点Python之comprehensions
每天学点Python之comprehensions 推导式能够简化对数据的处理,让代码简洁的同一时候还具有非常高的可读性.这在Python中非经常见. 列表推导式 通过列表推导式能够对列表中的全部元素 ...
- POJ 2296 Map Labeler(2-sat)
POJ 2296 Map Labeler 题目链接 题意: 坐标轴上有N个点.要在每一个点上贴一个正方形,这个正方形的横竖边分别和x,y轴平行,而且要使得点要么在正方形的上面那条边的中点,或者在以下那 ...
- 手势跟踪论文学习:Realtime and Robust Hand Tracking from Depth(三)Cost Function
iker原创.转载请标明出处:http://blog.csdn.net/ikerpeng/article/details/39050619 Realtime and Robust Hand Track ...
- PHP的curl库代码使用
欢迎訪问个人原创地址: http://www.phpthinking.com/archives/468 使用PHP的cURL库能够简单和有效地去抓网页. 你仅仅须要执行一个脚本.然后分析一下你所抓取的 ...
- spring web mvc第一天
spring web mvc 感觉就是高大上啊!啥都是配置文件就能够了.所以第一步就是弄清楚配置文件使用和总体框架的流程! Spring web mvc最重要的当然是Controller,也就是首先 ...
- 计算机网络 4.网络层与IP协议
网络中的每一台主机和路由器都有一个网络层部分.而路由器中也没有网络层以上的层次.网络层是协议栈中最复杂的层次. 转发forwarding:当一个分组到达某路由器的输入链路时.该路由器将分组移动到适当的 ...
- 自己定义CNN网络模型并使用caffe训练
caffe自带的例子中对mnist手写体数字训练使用的卷积神经网络是在lenet_train_test.prototxt中定义的,隐含层包含了2个卷积层,2个池化层,2个全连接层,1个激活函数层.网络 ...
- 判断一个整数是否是回文数C++实现 leetcode系列(九)
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...