CF277E Binary Tree on Plane

题目大意

给定平面上的 \(n\) 个点,定义两个点之间的距离为两点欧几里得距离,求最小二叉生成树。

题解

妙啊。

难点在于二叉的限制。

注意到二叉树每一个点最多有一个父亲,最多可以有两个儿子,这让我们联想到了网络流中的容量。

考虑建图:

令源点为 \(s\),汇点为 \(t\)。

我们将每一个点 \(u\) 拆为两个点 \(u_1,u_2\);

每个点最多可以有两个儿子 \(\Rightarrow\) 从 \(s\) 向 \(u_1\) 连一条容量为 \(2\),费用为 \(0\) 的边。

每个点最多可以有一个父亲 \(\Rightarrow\) 从 \(u_2\) 向 \(t\) 连一条容量为 \(1\),费用为 \(0\) 的边。

\(u\) 可以成为 \(v\) 的父亲 \(\Rightarrow\) 从 \(u_1\) 向 \(v_2\) 连一条容量为 \(1\),费用为 \(\operatorname{dist}(u,v)\) 的边。

然后我们跑最小费用最大流,判断最大流是否为 \(n-1\) 即可。

/*---Author:HenryHuang---*/
#include<bits/stdc++.h>
#define eps 1e-6
using namespace std;
const int maxn=800+5;
typedef long long ll;
struct edge{
int to,nex,w;
double v;
}e[maxn*maxn*2];
int head[maxn],cur[maxn],tot=1;
void add(int a,int b,int c,double d){
e[++tot]=(edge){b,head[a],c,d};
head[a]=cur[a]=tot;
}
void addedge(int a,int b,int c,double d){
add(a,b,c,d);
add(b,a,0,-d);
}
int n,m,s,t;
double dis[maxn];
bool vis[maxn];
bool spfa(){
memset(vis,0,sizeof vis);
for(int i=s;i<=t;++i) vis[i]=0,dis[i]=(1ll<<60),cur[i]=head[i];
queue<int> Q;
dis[s]=0;vis[s]=1;Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
vis[u]=0;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(e[i].w&&dis[v]-dis[u]-e[i].v>eps){
dis[v]=dis[u]+e[i].v;
if(!vis[v]) vis[v]=1,Q.push(v);
}
}
}
return dis[t]<(double)(1ll<<60);
}
int dfs(int u,int in){
if(u==t) return in;
int out=0,tmp;
vis[u]=1;
for(int i=cur[u];i;i=e[i].nex){
int v=e[i].to;cur[u]=i;
if((!vis[v])&&e[i].w&&abs(dis[v]-dis[u]-e[i].v)<eps&&(tmp=dfs(v,min(in,e[i].w)))){
e[i].w-=tmp,e[i^1].w+=tmp;
in-=tmp,out+=tmp;
if(!in) break;
}
}
if(!out) dis[u]=0;
vis[u]=0;
return out;
}
double x[maxn],y[maxn];
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i) cin>>x[i]>>y[i];
s=0,t=2*n+1;
for(int i=1;i<=n;++i) addedge(s,i,2,0),addedge(i+n,t,1,0);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(i!=j&&y[i]>y[j]) addedge(i,j+n,1,sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
int ans1=0;double ans2=0;
while(spfa()){
int tmp=dfs(s,2e9);
ans2+=dis[t]*tmp;
ans1+=tmp;
}
if(ans1==n-1) cout<<setprecision(6)<<fixed<<ans2<<'\n';
else cout<<-1<<'\n';
return 0;
}

CF277E Binary Tree on Plane的更多相关文章

  1. 题解【CF277E Binary Tree on Plane】

    Description 给你平面上 \(n\) 个点 \((2 \leq n \leq 400)\),要求用这些点组成一个二叉树(每个节点的儿子节点不超过两个),定义每条边的权值为两个点之间的欧几里得 ...

  2. CF 277E Binary Tree on Plane (拆点 + 费用流) (KM也可做)

    题目大意: 平面上有n个点,两两不同.现在给出二叉树的定义,要求树边一定是从上指向下,即从y坐标大的点指向小的点,并且每个结点至多有两个儿子.现在让你求给出的这些点是否能构成一棵二叉树,如果能,使二叉 ...

  3. Codefoces 277 E. Binary Tree on Plane

    题目链接:http://codeforces.com/problemset/problem/277/E 参考了这篇题解:http://blog.csdn.net/Sakai_Masato/articl ...

  4. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  8. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  9. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

随机推荐

  1. IDEA 通过ctrl+滚轮缩放字体大小

    能用图解决的问题,尽量简单粗暴通俗易懂 1.第一种方式 2.第二种方式

  2. redis主从架构宕机问题手动解决

    1    主机宕机 1.  设置端口6379是主机,端口6380是从机,全部都正常启动 2.  验证在6379写入数据,在6380也能得到数据 3.  现在将6379主机停掉,模拟主机宕机 4.  由 ...

  3. Jmeter 录制 https协议是出现“您访问的不是安全链接”提示时

    解决方法参考: https://blog.csdn.net/test_leader/article/details/112274549

  4. VB 老旧版本维护系列---迷之集合- ArrayList

    迷之集合- ArrayList '定义一个字符串 Dim dataType_ImageStr As String = "2023,2091,2092,2096,2212" '将字符 ...

  5. 虹软人脸识别SDK接入Milvus实现海量人脸快速检索

    一.背景 人脸识别是近年来最热门的计算机视觉领域的应用之一,而且现在已经出现了非常多的人脸识别算法,如:DeepID.FaceNet.DeepFace等等.人脸识别被广泛应用于景区.客运.酒店.办公室 ...

  6. Java-数组拷贝

    数组拷贝 首先了解深拷贝 浅拷贝数组的四种拷贝方式: 1.for循环拷贝 代码示例: import java.util.Arrays; public class TestDemo{ public st ...

  7. 深入理解java虚拟机笔记补充-JVM常见参数设置

    JVM 常见参数设置 内存设置 参数 -Xms:初始堆大小,JVM 启动的时候,给定堆空间大小. -Xmx:最大堆大小,如果初始堆空间不足的时候,最大可以扩展到多少. -Xmn:设置年轻代大小.整个堆 ...

  8. 9种设计模式在Spring中的运用,一定要非常熟练

    1.简单工厂(非23种设计模式中的一种) 实现方式: BeanFactory.Spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获得Bean对象,但是否是在传入参数 ...

  9. cos中的文件结构(DF/EF/MF/FID/AID/SFI..)

    转载自:https://blog.csdn.net/Enjoy_endless/article/details/75108349 刚开始接触CPU卡的时候,对于各种文件.应用的定义容易模糊不清,通常不 ...

  10. Unity_DOTween

    DOTween官网 DG.Tweening.Ease枚举详解 2019.12.12补充: 问题:当前dotween动画没播放完,便再次播放有冲突的操作,如连续多次播放.正播.倒播,导致显示不正常或报错 ...