Codefoces 277 E. Binary Tree on Plane
题目链接:http://codeforces.com/problemset/problem/277/E
参考了这篇题解:http://blog.csdn.net/Sakai_Masato/article/details/50775315
没看出来是费用流啊...我好菜啊。
我写得有一点和上面这篇题解不同:在判断是否无解时我直接记录最大流,判断最大流是否等于$n-1$(似乎是等价的...)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1010
#define inf 0x7fffffff
#define llg int
#define sqr(_) ((_)*(_))
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m; struct node
{
llg u,v,c,next;
double w;
}; struct FLOW
{
llg cnt,maxflow,S,T,pre[maxn],N;
llg head[maxn],dl[maxn*maxn];
bool bj[maxn];
double mincost,dis[maxn];
node e[maxn*maxn]; void init()
{
memset(head,-,sizeof(head));
mincost=cnt=maxflow=;
maxflow=; mincost=;
} void link(llg u,llg v,double w,llg c)
{
e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].c=c;
e[cnt].next=head[u],head[u]=cnt++; e[cnt].u=v,e[cnt].v=u,e[cnt].w=-w,e[cnt].c=;
e[cnt].next=head[v],head[v]=cnt++;
} void updata()
{
llg f=inf;
for (llg i=T;i!=S;i=e[pre[i]].u) f=min(f,e[pre[i]].c);
maxflow+=f;
for (llg i=T;i!=S;i=e[pre[i]].u)
{
e[pre[i]].c-=f;
e[pre[i]^].c+=f;
mincost+=(double)f*e[pre[i]].w;
}
} bool spfa()
{
llg u,v;
double w;
memset(pre,-,sizeof(pre));
memset(bj,,sizeof(bj));
for (llg i=;i<=N;i++) dis[i]=inf;
llg l=,r=;
dl[r]=S; bj[S]=; dis[S]=;
do
{
bj[u=dl[++l]]=;
for (llg i=head[u];i!=-;i=e[i].next)
{
v=e[i].v,w=e[i].w;
if (e[i].c && dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pre[v]=i;
if (!bj[v]) bj[v]=,dl[++r]=v;
}
}
}while (l<r); if (pre[T]==-) return ;
return ;
} void work()
{
while (spfa())
updata();
} }G; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} struct POINT{double x,y;}po[maxn]; bool cmp(const POINT&x,const POINT&y) {return x.y==y.y?x.x<y.x:x.y<y.y;} double dis(const POINT&x,const POINT&y) {return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y));} int main()
{
yyj("flow");
cin>>n;
G.init();
G.N=n*+;
for (llg i=;i<=n;i++) scanf("%lf%lf",&po[i].x,&po[i].y);
sort(po+,po+n+,cmp);
reverse(po+,po++n);
for (llg i=;i<=n;i++)
for (llg j=i+;j<=n;j++)
if (po[i].y>po[j].y)
G.link(i,j+n,dis(po[i],po[j]),(llg));
G.S=*n+,G.T=*n+;
for (llg i=;i<=n;i++)
{
G.link(G.S,i,,(llg));
G.link(i+n,G.T,,(llg));
}
G.work();
if (G.maxflow!=n-) {cout<<-;} else printf("%.9lf",G.mincost);
return ;
}
Codefoces 277 E. Binary Tree on Plane的更多相关文章
- CF277E Binary Tree on Plane
CF277E Binary Tree on Plane 题目大意 给定平面上的 \(n\) 个点,定义两个点之间的距离为两点欧几里得距离,求最小二叉生成树. 题解 妙啊. 难点在于二叉的限制. 注意到 ...
- CF 277E Binary Tree on Plane (拆点 + 费用流) (KM也可做)
题目大意: 平面上有n个点,两两不同.现在给出二叉树的定义,要求树边一定是从上指向下,即从y坐标大的点指向小的点,并且每个结点至多有两个儿子.现在让你求给出的这些点是否能构成一棵二叉树,如果能,使二叉 ...
- 题解【CF277E Binary Tree on Plane】
Description 给你平面上 \(n\) 个点 \((2 \leq n \leq 400)\),要求用这些点组成一个二叉树(每个节点的儿子节点不超过两个),定义每条边的权值为两个点之间的欧几里得 ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- 常用基础Linux操作命令总结与hadoop基础操作命令
cd命令:切换目录 (1)切换到目录 /usr/local cd /usr/local (2)去到目前的上层目录 cd .. (3)回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4)查看目 ...
- PHP(控制语句,随机数,循环语法)
1.随机数:Math.random():0到1 不包括1 永远取不到2.控制语句if(){} for循环 语法 运行步骤(过程,原理) 1.初始化 2.判断条件 3.变量改变 index:下 ...
- 求最小生成树的kruskal算法
连通无向图有最小生成树,边权从小到大排序,每次尝试加入权最小的边,如果不成圈,就把这边加进去,所有边扫一遍就求出了最小生成树. 判断连通分支用Union-Set(并查集),就是把连通的点看成一个集合, ...
- 手写AVL 树(上)
平衡二叉树 左旋,右旋,左右旋,右左旋 具体原理就不说了,网上教程很多.这里只实现了建树的过程,没有实现删除节点的操作. 下一篇会实现删除节点的操作. // // main.cpp // AVL // ...
- MacOS High Sierra 引起 VirtualBox Vagrant 同步慢
问题 最近把mac的操作系统升级到了最新版本发现了一个问题,通过共享文件夹的方式 修改的文件,无法立即同步到虚拟机中,大概需要30秒才能同步到共享文件夹. 操作环境如下 虚拟机:Virtualbox ...
- poj3278
#include<iostream> #define MAX 100001 int john,cow; int queue[MAX]; int vis[MAX]; int ans; voi ...
- 《PHP - CGI/Fastcgi/PHP-FPM》
先说下我最近看到的一篇文章,哈哈哈,特别好玩. 一步步教你编写不可维护的 PHP 代码 之前一直知道 PHP 在 CGI 模式下运行.命令行下在 CLI 模式下运行. 但是 FPM 和 nginx 配 ...
- Linux下Solr单机版、集群版安装与配置
一.安装 1.需要的安装包有apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 这里默认大家已经安装好jdk与tomcat,所以在这里不做 ...
- 11.0-uC/OS-III就绪列表(优先级)
准备运行的任务被放置于就绪列表中.就绪列表包括2个部分:位映像组包含了优先级信息,一个表包含了所有指向就绪任务的指针. 1.优先级 图6-1到6-3显示了优先级的位映像组.它的宽度取决于CPU_DAT ...
- C#编程基础(简单概述与理解)
1.C#变量和数据输入 C#常用到的几个数据类型: 整型:int 说明:32位有符号整数 范围:-2³¹~2³¹-1 浮点型:double 说明:64位双精度浮点数 范围:±5.0×10-﹣³²~± ...