题解【CF277E Binary Tree on Plane】
Description
给你平面上 \(n\) 个点 \((2 \leq n \leq 400)\),要求用这些点组成一个二叉树(每个节点的儿子节点不超过两个),定义每条边的权值为两个点之间的欧几里得距离。求一个权值和最小的二叉树,并输出这个权值。
其中,点 \(i\) 可以成为点 \(j\) 的的父亲的条件是:点 \(i\) 的 \(y\) 坐标比 \(j\) 的 \(y\) 坐标大。
如果不存在满足条件的二叉树,输出 \(-1\) 。
Solution
边 \((a,b)\) 表示一条容量为 \(a\) ,费用为 \(b\) 的边
把每个点 \(u\) 拆成两个点入点 \(u_1\) 和出点 \(u_2\)
从源点向 \(u_1\) 连一条 \((2,0)\),意义为限制了 \(u\) 只能有两个儿子
从 \(u_2\) 向汇点连一条 \((1,0)\) ,意义是限制了 \(u\) 最多只有一个父亲
若 \(u_y > v_y\) 则在 \(u_1\) 和 \(v_2\) 之间连一条 \((1,Len)\),其中 Len 是两点之间的距离 \(\sqrt {(u_x - v_x)^2 + (u_y-v_y)^2}\)
然后跑最小费用最大流完事。
Code
#include <bits/stdc++.h>
#define db double
using namespace std;
const int N = 450;
const int INF = 1000000000;
int n, k, S, T, vis[N], cnt, f[N * 2], pre[N * 2];
db dis[N * 2];
struct edge {
int v, f; db w; edge *next, *rev;
}pool[N * N], *head[N * 2], *r[N * 2];
struct node {
int sid, tid;
db x, y;
}a[N];
inline db Len(db x1, db y1, db x2, db y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
inline void addedge(int u, int v, int f, db w) {
edge *p = &pool[++cnt], *q = &pool[++cnt];
p->v = v, p->f = f, p->w = w, p->next = head[u], head[u] = p; p->rev = q;
q->v = u, q->f = 0, q->w = -w, q->next = head[v], head[v] = q; q->rev = p;
}
inline bool spfa() {
for(int i = S; i <= T; i++) pre[i] = -1, dis[i] = INF, r[i] = NULL, vis[i] = 0;
queue <int> Q; Q.push(S), dis[S] = 0, vis[S] = 1; f[S] = INF;
while(!Q.empty()) {
int u = Q.front(), v; Q.pop(); vis[u] = 0;
for(edge *p = head[u]; p; p = p->next) {
if(p->f && dis[v = p->v] > dis[u] + p->w) {
dis[v] = dis[u] + p->w;
pre[v] = u, r[v] = p;
f[v] = min(f[u], p->f);
if(!vis[v]) vis[v] = 1, Q.push(v);
}
}
} return pre[T] != -1;
} int MF; db MC;
inline void MCMF() {
while(spfa()) {
for(int i = T; i != S; i = pre[i])
r[i]->f -= f[T], r[i]->rev->f += f[T];
MF += f[T], MC += 1.0 * dis[T] * f[T];
}
}
int main() {
scanf("%d", &n); S = 0, T = 2 * n + 1;
for(int i = 1; i <= n; i++) {
scanf("%lf %lf", &a[i].x, &a[i].y);
a[i].sid = i * 2 - 1, a[i].tid = i * 2;
addedge(S, a[i].sid, 2, 0);
addedge(a[i].tid, T, 1, 0);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i != j && a[i].y > a[j].y)
addedge(a[i].sid, a[j].tid, 1, Len(a[i].x, a[i].y, a[j].x, a[j].y));
MCMF();
if(MF == n - 1) printf("%lf\n", MC);
else printf("-1\n");
return 0;
}
题解【CF277E Binary Tree on Plane】的更多相关文章
- CF277E Binary Tree on Plane
CF277E Binary Tree on Plane 题目大意 给定平面上的 \(n\) 个点,定义两个点之间的距离为两点欧几里得距离,求最小二叉生成树. 题解 妙啊. 难点在于二叉的限制. 注意到 ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- [LeetCode 题解]: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- CF 277E Binary Tree on Plane (拆点 + 费用流) (KM也可做)
题目大意: 平面上有n个点,两两不同.现在给出二叉树的定义,要求树边一定是从上指向下,即从y坐标大的点指向小的点,并且每个结点至多有两个儿子.现在让你求给出的这些点是否能构成一棵二叉树,如果能,使二叉 ...
- Codefoces 277 E. Binary Tree on Plane
题目链接:http://codeforces.com/problemset/problem/277/E 参考了这篇题解:http://blog.csdn.net/Sakai_Masato/articl ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- LeetCode题解之Binary Tree Pruning
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...
- LeetCode题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
随机推荐
- java使用jacob将office文档转换为PDF格式
jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 微软职位内部推荐-Software Engineer II_HPC
微软近期Open的职位: Job Title: Software Engineer II_HPC Location: Shanghai, China Are you passionate about ...
- Linux 150命令之查看文件及内容处理命令 cat tac less head tail cut
cat 查看文件内容 [root@mysql tmp]# cat 2.txt 1234 -n 查看行号 [root@mysql tmp]# cat -n 2.txt 1 1234 ...
- gitLab服务器搭建+ rundeck自动化部署
git服务器搭建 https://blog.csdn.net/gx_1_11_real/article/details/79406427 rundeck 部署 https://blog.csdn. ...
- hdu1242 Rescue DFS(路径探索题)
这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...
- C++ Primer Plus学习:第六章
C++入门第六章:分支语句和逻辑运算符 if语句 语法: if (test-condition) statement if else语句 if (test-condition) statement1 ...
- Alpha阶段敏捷冲刺 ADY8
一.举行站立式例会 今天也没有拍照片,人不齐. 二.团队报告 1.昨日已完成的工作 (1)创建一个test,并且将图片导入进去使其可以显示. 2.今日计划完成的工作 完成收尾工作.实现代码的连接. 3 ...
- (转)Linux 命令--查看物理CPU个数、核数、逻辑CPU个数
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...
- session的基本原理
转载:http://blog.sina.com.cn/s/blog_8155e74d0101iqmh.html 如何严格限制session在30分钟后过期! 1.设置客户端cookie的lifetim ...