题目链接: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的更多相关文章

  1. CF277E Binary Tree on Plane

    CF277E Binary Tree on Plane 题目大意 给定平面上的 \(n\) 个点,定义两个点之间的距离为两点欧几里得距离,求最小二叉生成树. 题解 妙啊. 难点在于二叉的限制. 注意到 ...

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

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

  3. 题解【CF277E Binary Tree on Plane】

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

  4. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

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

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

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

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

  7. Leetcode 笔记 110 - Balanced Binary Tree

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

  8. 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 ...

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

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

随机推荐

  1. Apache hardoop 的基础知识学习总结

    hardoop的主要构成: (1)HDFS 分布式文件系统,解决海量数据存储 (2) YARN 解决资源任务调度(包括作业调度和集群资源调度) (3)MapReduce 解决海量数据计算 集群搭建的步 ...

  2. Ubuntu常用操作命令

    解压文件: tar -zxvf 文件名 -C 指定目录 从当前环境进入root环境: su,然后输入root密码

  3. 实现mybash

    任务内容 1.使用fork,exec,wait实现mybash 查找资料: fork函数 通过fork()系统调用我们可以创建一个和当前进程印象一样的新进程.我们通常将新进程称为子进程,而当前进程称为 ...

  4. C++的默认构造函数

    待看文章:C++ 合成默认构造函数的真相 默认构造函数指不带参数或者所有参数都有缺省值的构造函数!!! 类的默认构造函数可以使得在实例化该类的对象时不用提供参数,但是类也可以不含默认构造函数,这样在实 ...

  5. python将字符串转换成整型

    将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...

  6. HBuilder/Mui开发ios使用上拉刷新导致滚动条无法使用的解决方法

    HBuilder/Mui开发的APP使用上拉刷新,当滚动到底部是会触发上拉刷新,加载更多数据.但是ios上确是一个坑,导致滚动条无法滚动. 解决方法 放弃Mui的上拉刷新,自己使用JS实现. var ...

  7. haier周的计算原则

    现使用oracle的sql表示出haier周, 经过对其生成结果的分析,发现海尔周是以周日到周六分别作为一周的始末, 用到的oracle sql中会涉及到calendar week的定义,还涉及到了I ...

  8. 基于Jenkins实现持续集成【持续更新中】

    持续集成 1.什么是持续集成:Continuous integration (CI)持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生 ...

  9. laravel容器container 阅读记录

    今天抽时间又仔细看了一下laravel的container,记录一下. 所谓容器,听名字就知道,是一个仓库,装东西用的,所以,container所有的功能,都围绕一个主题:管理装. 类名称:Illum ...

  10. 使用fastdfs搭建文件服务器

    一:安装tracker 1. 拷贝安装目录下各个.gz文件到/usr/local/src下,解压各个install lib,例如tar zxvf xxx.tar.gz 2. 先安装libfastcom ...