#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> using namespace std; const int maxn = 1011; const int maxm = 1000010; const int inf = 100000000; struct SAP{ struct Edge{ int v, next; int w; }edge[maxm]; int head[maxn]; int cnt; int n, s, t;//n = t + 1; int pre[maxn],cur[maxn],dis[maxn],gap[maxn]; void add(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int sap() { int flow=0,aug=inf; int u; bool flag; for(int i=0;i<=n;i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } void init(int __n,int __s, int __t) { n = __n; s = __s; t = __t; cnt = 0; memset(head,-1,sizeof(head)); } }Flow;