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 ...
随机推荐
- python全栈开发 * 进程池,线程理论 ,threading模块 * 180727
一.进程池 (同步 异步 返回值) 缺点: 开启进程慢 几个CPU就能同时运行几个程序 进程的个数不是无线开启的 应用: 100个任务 进程池 如果必须用多个进程 且是高计算型 没有IO型的程序 希望 ...
- 用户场景分析i
名字 学生(注重饮食选择,挑剔) 年龄 20 收入 无 知识层面 大学 使用这个网站的典型场景 中午或者晚上饿了但是不知道想吃什么,又不想随便吃,还是比较挑剔..这时,他就需要通过我们的网站来看其他人 ...
- 找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.应用程序类必 须扩展javafx.application.Application”
用eclipse写代码的时候,写了一个简单的程序,编译的时候突然出现“错误: 在类 com.test.demo 中找不到 main 方法, 请将 main 方法定义为: public static v ...
- Socket 的网络编程
socket 网络编程的服务端: 1) 创建socket 套接字. 2) 和socket绑定主机地址和端口 3) socket 主动监听端口,看又没有来连接. 4) 当执行到 accept() 时, ...
- 转:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述
原文地址:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述 ArcGIS JavaScript API允许在地图上绘制graphic(图形) ...
- jsp (二) 练习
package cn.sasa.serv; import java.io.IOException; import java.sql.SQLException; import java.util.Lis ...
- select()函数 的学习
select()的介绍 全是拷贝的如下文章: https://www.cnblogs.com/wenqiang/p/5508541.html select()函数的用例代码摘录如下文章: https: ...
- [面试题]vi/vim快捷键及面试题系列
选择 vi保存退出命令 w! wq! q! www vi移动光标到文件最后一行 G g ggg 4444 vi删除一行的命令 dd d D shift+4 在vi编辑器中的命令模式下,键入()可在光标 ...
- 去掉Tomcat的管理页面
一.去掉Tomcat的管理页面 一.方法一:如果要去掉默认该界面,可以重命名tomcat目录下的ROOT,并新建空文件夹命名为ROOT 1.刚打开tomcat,默认访问的是tomcat管理页面,比如X ...
- 小程序 切换到tabBar页面不刷新问题
小程序跳转的几种方式有wx.navigateTo,wx.redirectTo,wx.reLaunch,wx.switchTab等.下面我们重点研究切换到tabBar的两种方式. wx.switchTa ...