Description

两个PACMAN吃豆豆。一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方。PACMAN走到豆豆处就会吃掉它。PACMAN行走的路线很奇怪,只能向右走或者向上走,他们行走的路线不可以相交。 请你帮这两个PACMAN计算一下,他们俩加起来最多能吃掉多少豆豆。

Input

第一行为一个整数N,表示豆豆的数目。 接下来 N 行,每行一对正整数,表示第i个豆豆的坐标。任意两个豆豆的坐标都不会重合。

Output

仅有一行包含一个整数,即两个PACMAN加起来最多能吃掉的豆豆数量

Sample Input

8

8 1

1 5

5 7

2 2

7 8

4 6

3 3

6 4

Sample Output

7

HINT

N < = 2000

Source

拼尽全力优化,最后还是超时1个点。。。

裸的费用流

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int INF=0x7fffffff; struct Edge
{
int from,to,v,c,next;
}E[];
int node=;
int head[],from[],dis[],vis[]; int n,ans,S,T;
struct point
{
int x,y;
}P[]; int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-f;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} void ins(int from,int to,int v,int c)
{
node++;
E[node]=(Edge){from,to,v,c,head[from]};
head[from]=node;
} void insert(int from,int to,int v,int c)
{
ins(from,to,v,c);ins(to,from,,-c);
} bool spfa()
{
queue<int> Q;
for(int i=;i<=T;i++) dis[i]=-INF;
Q.push();dis[]=;vis[]=;
while(!Q.empty())
{
int q=Q.front();Q.pop();
for(int i=head[q];i;i=E[i].next)
if(E[i].v>&&dis[q]+E[i].c>dis[E[i].to])
{
dis[E[i].to]=dis[q]+E[i].c;
from[E[i].to]=i;
if(!vis[E[i].to])
{
Q.push(E[i].to);
vis[E[i].to]=;
}
}
vis[q]=;
}
return dis[T]!=-INF;
} void mcf()
{
int x=INF;
for(int i=from[T];i;i=from[E[i].from])
x=min(E[i].v,x);
for(int i=from[T];i;i=from[E[i].from])
{
ans+=x*E[i].c;
E[i].v-=x;E[i^].v+=x;
}
} bool cmp(point a,point b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
} int main()
{
n=read();T=*n+;S=T-;
for(int i=;i<=n;i++)
P[i].x=read(),P[i].y=read();
sort(P+,P+n+,cmp);
insert(,S,,);
for(int i=;i<=n;i++)
{
insert(S,i,,);
insert(i,i+n,,);
insert(i,i+n,,);
insert(i+n,T,,);
}
for(int i=;i<=n;i++)
{
int inf=INF;
for(int j=i+;j<=n;j++)
{
if(P[i].y<=P[j].y)
{
if(P[j].y<inf)
insert(i+n,j,,);
inf=min(inf,P[j].y);
}
}
}
while(spfa())
mcf();
printf("%d",ans);
return ;
}

最新文章

  1. Go - 函数/方法 的 变参
  2. [.NET] CErrStack 方便地管理错误或异常
  3. Foundation -----&gt;NSDictionary
  4. Thoughts on an Article from Science &#39;A network framework of cultural history&#39;
  5. 【原】整理的react相关的一些学习地址,包括 react-router、redux、webpack、flux
  6. HDU 3448 Bag Problem
  7. OS_TASK.C
  8. merge into sql优化
  9. 谈谈Facebook的聊天系统架构
  10. multi lstm attention 坑一个
  11. u-boot移植(十三)---代码修改---支持文件系统及补丁制作
  12. Linux笔记之如何分割文件或管道流:split
  13. NOI 8467 鸣人的影分身
  14. 八、curator recipes之选举主节点LeaderSelector
  15. ubuntu中wifi显示被硬件禁用的解决方法
  16. A Neural Algorithm of Artistic Style
  17. cdh ntpdate 问题
  18. ue4 c++ anim notify
  19. Echarts学习总结(一)-----柱状图
  20. spring 如何动态加载properties文件的内容

热门文章

  1. pytest框架(四)
  2. UIActionSheet的最后一项点击失效
  3. Python列表与元组
  4. 收藏 SpringBoot :thymeleaf 使用详解
  5. (转)linux实战考试题:批量创建用户和密码-看看你会么?
  6. Spring注入属性、对象
  7. ParallelsDesktop安装DOS7.1并与MAC共享文件
  8. 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因
  9. 如何用Windows PowerShell替换命令提示符
  10. (转载)资源字典(Pro WPF 学习)