标签:line ret hint == 时间 ++ space geo man
N < = 2000
#include<cmath> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; struct node{ int x,y,c,d,next,other; }a[2100000];int len,last[410000]; const int INF=99999999; void ins(int x,int y,int c,int d) { int k1,k2; k1=++len; a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1; } int st,ed; int head,tail,list[41000]; int pre[41000],cc[41000],d[41000]; bool v[41000]; int ans,flow; bool spfa() { for(int i=1;i<=ed+2;i++)d[i]=-1; d[st]=0; memset(cc,0,sizeof(cc));cc[st]=9999999; memset(v,false,sizeof(v));v[st]=true; list[1]=st;head=1;tail=2; while(head!=tail) { int x=list[head]; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(d[y]<d[x]+a[k].d&&a[k].c>0) { d[y]=d[x]+a[k].d; cc[y]=min(cc[x],a[k].c); pre[y]=k; if(v[y]==false) { v[y]=true; list[tail++]=y; if(tail==ed+1)tail=1; } } } v[x]=false; head++;if(head==ed+1)head=1; } if(d[ed]==-1)return false; ans+=cc[ed]*d[ed];flow+=cc[ed]; int x=ed; while(x!=0) { int k=pre[x]; a[k].c-=cc[ed],a[a[k].other].c+=cc[ed]; x=a[k].x; } return true; } struct zuobiao{ int x,y; }e[210000]; int n; bool cmp(zuobiao a,zuobiao b) { if(a.x!=b.x)return a.x<b.x; return a.y<b.y; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&e[i].x,&e[i].y); len=0;memset(last,0,sizeof(last)); st=2*n+1,ed=2*n+2; int S=ed+1,T=S+1; ins(st,S,2,0);ins(T,ed,2,0); for(int i=1;i<=n;i++)ins(S,i,1,0),ins(i+n,T,1,0),ins(i,i+n,1,1),ins(i,i+n,1,0); sort(e+1,e+1+n,cmp); for(int i=1;i<=n;i++) { int x=0; for(int j=i-1;j>=1;j--) if(e[i].y>=e[j].y&&e[j].y>x) x=e[j].y,ins(j+n,i,2,0); } ans=0; while(spfa()==true){} printf("%d\n",ans); return 0; }
by_lmy
BZOJ1930: [Shoi2003]pacman 吃豆豆
标签:line ret hint == 时间 ++ space geo man
原文地址:https://www.cnblogs.com/MT-LI/p/8849597.html