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. GO学习-(15) Go语言基础之包

    Go语言基础之包 在工程化的Go语言开发项目中,Go语言的源码复用是建立在包(package)基础之上的.本文介绍了Go语言中如何定义包.如何导出包的内容及如何导入其他包. Go语言的包(packag ...

  2. CVPR2020最新论文扫描盘点(上)

    CVPR2020最新论文扫描盘点(上) 最近计算机视觉三大顶会之一CVPR2020接收结果已经公布,一共有1470篇论文被接收,接收率为22%,相比去年降低3个百分点,竞争越来越激烈.这里整理来自Tw ...

  3. A100 GPU硬件架构

    A100 GPU硬件架构 NVIDIA GA100 GPU由多个GPU处理群集(GPC),纹理处理群集(TPC),流式多处理器(SM)和HBM2内存控制器组成. GA100 GPU的完整实现包括以下单 ...

  4. 嵌入式Linux的OTA更新,基础知识和实现

    嵌入式Linux的OTA更新,第1部分-基础知识和实现 OTA updates for Embedded Linux,  Fundamentals and implementation 更新的需要 一 ...

  5. ContOS8 使用yum安装MariaDB

    首先全部删除MySQL/MariaDB(若是首次安装可根据需要跳过此步) 若不清楚MySQL和MariaDB的关系请移步至 Mariadb百科 1.查看系统版本(以下任一命令即可). # cat /p ...

  6. spring整合mybatis,ioc容器及声明式事务配置

    步骤: 1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息 jdbc.properties:jdbc.user=root jdbc.password=123456 jdbc. ...

  7. Linkerd 2.10(Step by Step)—2. 自动化的金丝雀发布

    通过结合 Linkerd 和 Flagger 来根据服务指标自动金丝雀(canary)发布,从而降低部署风险. Linkerd 2.10 中文手册持续修正更新中: https://linkerd.ha ...

  8. 【NX二次开发】Block UI 线宽

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  9. Kubernetes 实战——有状态应用(StatefulSet)

    一.简介 有状态实例:新实例和旧实例需要有相同的名称.网络标识和状态 无状态实例:可随时被替换 1. ReplicaSet 和有状态 Pod ReplicaSet 通过 Pod 模板创建多个 Pod ...

  10. 分分钟教你Python Web开发框架Django

    Python除了爬虫.深度学习(人工智能).数据分析等外,还可以用来开发网站系统,如我们常见的知乎,豆瓣等都是用Python开发的网站系统. 今天辰哥就来教大家如何新建属于自己的Django项目,让D ...