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. 排查利器:Tcpdump抓包 & Wireshark解析

    在工作这一块,免不了和其他开发人员打交道.比如,和其他部门 or 公司联调,甚至是和自己部门的人联调的时候.这时候,对接问题就很容易暴露出来,特别是Tcp/Udp会话的时候,很容易就会呈现出公说公有理 ...

  2. HDFS 05 - HDFS 的元数据管理(FSImage、EditLog、Checkpoint)

    目录 1 - NameNode 的启动流程 2 - NameNode 的元数据 2.1 EditLog 操作日志 2.2 查看 EditLog 文件 2.3 FSImage 元数据镜像 2.4 查看 ...

  3. 批量执行异步任务CompletionService

    批量执行异步任务CompletionService 核心思想,就是将异步结果放入到阻塞队列中,然后再消费队列,实现异步任务批量执行 接口方法说明 Future<V> submit(Call ...

  4. 通过 DLPack 构建跨框架深度学习编译器

    通过 DLPack 构建跨框架深度学习编译器 深度学习框架,如Tensorflow, PyTorch, and ApacheMxNet,快速原型化和部署深度学习模型提供了强大的工具箱.不幸的是,易用性 ...

  5. 前台JS遍历Table的所有单元格数据内容

    在日常开发过程中为了减少与后台服务器的交互次数,大部分的功能都会放到前台使用JS来完成. 例如:一个表中有ID(FOCUS_SEQ)和Name(COLUMNCTRL)两个字段,其中ID是自定义连续增长 ...

  6. JVM系列(五):gc实现概要01

    java的一大核心特性,即是自动内存回收.这让一些人从繁琐的内存管理中解脱出来,但对大部分人来说,貌似这太理所当然了.因为现在市场上的语言,几乎都已经没有了还需要自己去管理内存这事.大家似乎都以为,语 ...

  7. Django(64)频率认证源码分析与自定义频率认证

    前言 有时候我们发送手机验证码,会发现1分钟只能发送1次,这是做了频率限制,限制的时间次数,都由开发者自己决定 频率认证源码分析 def check_throttles(self, request): ...

  8. 【VBA】打开关闭工作簿等

    打开关闭工作簿等 1 Sub 打开工作簿() 2 Dim sFilePath As String 3 sFilePath = "D:\A.xls" 4 Dim oWB As Wor ...

  9. Linux中测试网络命令

    ping IP -t 是持续性查看网络状态

  10. python基本函数增删改排序,用range()求和

    a=["blue","red","brack"] print(len(a))#列表长度 a.append("yellow" ...