题目链接:http://codeforces.com/problemset/problem/277/E


参考了这篇题解:http://blog.csdn.net/Sakai_Masato/article/details/50775315

没看出来是费用流啊...我好菜啊。

我写得有一点和上面这篇题解不同:在判断是否无解时我直接记录最大流,判断最大流是否等于$n-1$(似乎是等价的...)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<cstdlib>
  6. #include<cmath>
  7. #include<cstring>
  8. using namespace std;
  9. #define maxn 1010
  10. #define inf 0x7fffffff
  11. #define llg int
  12. #define sqr(_) ((_)*(_))
  13. #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
  14. llg n,m;
  15.  
  16. struct node
  17. {
  18. llg u,v,c,next;
  19. double w;
  20. };
  21.  
  22. struct FLOW
  23. {
  24. llg cnt,maxflow,S,T,pre[maxn],N;
  25. llg head[maxn],dl[maxn*maxn];
  26. bool bj[maxn];
  27. double mincost,dis[maxn];
  28. node e[maxn*maxn];
  29.  
  30. void init()
  31. {
  32. memset(head,-,sizeof(head));
  33. mincost=cnt=maxflow=;
  34. maxflow=; mincost=;
  35. }
  36.  
  37. void link(llg u,llg v,double w,llg c)
  38. {
  39. e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].c=c;
  40. e[cnt].next=head[u],head[u]=cnt++;
  41.  
  42. e[cnt].u=v,e[cnt].v=u,e[cnt].w=-w,e[cnt].c=;
  43. e[cnt].next=head[v],head[v]=cnt++;
  44. }
  45.  
  46. void updata()
  47. {
  48. llg f=inf;
  49. for (llg i=T;i!=S;i=e[pre[i]].u) f=min(f,e[pre[i]].c);
  50. maxflow+=f;
  51. for (llg i=T;i!=S;i=e[pre[i]].u)
  52. {
  53. e[pre[i]].c-=f;
  54. e[pre[i]^].c+=f;
  55. mincost+=(double)f*e[pre[i]].w;
  56. }
  57. }
  58.  
  59. bool spfa()
  60. {
  61. llg u,v;
  62. double w;
  63. memset(pre,-,sizeof(pre));
  64. memset(bj,,sizeof(bj));
  65. for (llg i=;i<=N;i++) dis[i]=inf;
  66. llg l=,r=;
  67. dl[r]=S; bj[S]=; dis[S]=;
  68. do
  69. {
  70. bj[u=dl[++l]]=;
  71. for (llg i=head[u];i!=-;i=e[i].next)
  72. {
  73. v=e[i].v,w=e[i].w;
  74. if (e[i].c && dis[v]>dis[u]+w)
  75. {
  76. dis[v]=dis[u]+w;
  77. pre[v]=i;
  78. if (!bj[v]) bj[v]=,dl[++r]=v;
  79. }
  80. }
  81. }while (l<r);
  82.  
  83. if (pre[T]==-) return ;
  84. return ;
  85. }
  86.  
  87. void work()
  88. {
  89. while (spfa())
  90. updata();
  91. }
  92.  
  93. }G;
  94.  
  95. inline int getint()
  96. {
  97. int w=,q=; char c=getchar();
  98. while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
  99. while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
  100. }
  101.  
  102. struct POINT{double x,y;}po[maxn];
  103.  
  104. bool cmp(const POINT&x,const POINT&y) {return x.y==y.y?x.x<y.x:x.y<y.y;}
  105.  
  106. double dis(const POINT&x,const POINT&y) {return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y));}
  107.  
  108. int main()
  109. {
  110. yyj("flow");
  111. cin>>n;
  112. G.init();
  113. G.N=n*+;
  114. for (llg i=;i<=n;i++) scanf("%lf%lf",&po[i].x,&po[i].y);
  115. sort(po+,po+n+,cmp);
  116. reverse(po+,po++n);
  117. for (llg i=;i<=n;i++)
  118. for (llg j=i+;j<=n;j++)
  119. if (po[i].y>po[j].y)
  120. G.link(i,j+n,dis(po[i],po[j]),(llg));
  121. G.S=*n+,G.T=*n+;
  122. for (llg i=;i<=n;i++)
  123. {
  124. G.link(G.S,i,,(llg));
  125. G.link(i+n,G.T,,(llg));
  126. }
  127. G.work();
  128. if (G.maxflow!=n-) {cout<<-;} else printf("%.9lf",G.mincost);
  129. return ;
  130. }

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. jq 点击按钮显示div,点击页面其他任何地方隐藏div

    css .bl_rencai_32{ float: left; height: 35px; line-height: 35px; } .bl_rencai_32 >input{ width: 3 ...

  2. node.js+ react + redux 环境搭建

    1.安装node.js 2. yarn init: 初始化,主要包含以下条目 name: 项目名 version: 版本号 description: 项目简要描述 entry point: 文件入口, ...

  3. 对Http协议基本原理的理解

    超文本传输协议 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是 ...

  4. Java应用之POI

    POI的简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. HSSF的概况 HSSF 是Horr ...

  5. 20175320 2018-2019-2 《Java程序设计》第5周学习总结

    20175320 2018-2019-2 <Java程序设计>第5周学习总结 教材学习内容总结 本周学习了教材的第六章的内容.在这章中介绍了接口与实现,着重讲了接口是如何定义并实现以及如何 ...

  6. python文件派生

    import time class Foo: x = 1 def __init__(self, y): self.y = y def __getattr__(self, item): # 没有的情况下 ...

  7. Markdown编辑工具及命令

    Markdown是一种可以使用普通文本编辑器编辑的标记语言,通过使用简单的编辑,可以使文本具有一定的格式. Typora是一款简介的Markerdown编辑器. 文本编辑语法: 标题: # 一阶标题 ...

  8. RoR - Introduction to Active Record

    Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...

  9. angular--解决angular图片加载失败问题

    基于angular4写的一个指令,在ionic3.x项目在用.因为加载图片超时等原因导致图片显示不出来,需要替换成默认或者指定图片 1.err-src.ts import { Directive,In ...

  10. 初步了解Fork/Join框架

    框架介绍 Fork/Join框架是Java 7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个子任务,最终汇总每个子任务的执行结果以得到大任务结果的框架.Fork/Join框架要完成两件事 ...