标签:pop bsp empty splay src oid lap memset class
#include<bits/stdc++.h> using namespace std; struct edge { int to,nxxt,len; }e[100100<<1]; int n,m,s,t; int head[100100],cnt=-1,b[100100]; inline void addedge(int u,int v,int w) { cnt++; e[cnt].to=v; e[cnt].len=w; e[cnt].nxxt=head[u]; head[u]=cnt; } inline void superadd(int u,int v,int w) { addedge(u,v,w); addedge(v,u,0); } int deep[100100]; inline bool bfs() { queue<int>q; q.push(s); memset(deep,-1,sizeof deep); deep[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];~i;i=e[i].nxxt) { if(e[i].len<=0)continue; int to=e[i].to; if(deep[to]==-1) { deep[to]=deep[u]+1; q.push(to); } } } return deep[t]!=-1; } int dfs(int u,int tar) { if(u==t)return tar; int sum=0; for(int i=b[u];~i;i=e[i].nxxt,b[u]=i) { if(e[i].len<=0)continue; int to=e[i].to; if(deep[to]==deep[u]+1) { int num=dfs(to,min(tar,e[i].len)); if(num<=0)continue; e[i].len-=num; e[i^1].len+=num; tar-=num; sum+=num; if(!tar)return sum; } } return sum; } int main() { memset(head,-1,sizeof head); scanf("%d%d%d%d",&n,&m,&s,&t); /*for(int i=1;i<=n;i++) { int num; scanf("%d",&num); for(int j=1;j<=num;j++) { int to; scanf("%d",&to); superadd(i,to+n,1); } } s=n+m+1,t=n+m+2; for(int i=1;i<=n;i++)superadd(s,i,1); for(int i=n+1;i<=n+m;i++)superadd(i,t,1);*/ for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); superadd(u,v,w); } int ans=0; while(bfs()) { memcpy(b,head,sizeof head); ans+=dfs(s,2.1e9); } printf("%d",ans); }
标签:pop bsp empty splay src oid lap memset class
原文地址:https://www.cnblogs.com/yanghaokun/p/10758125.html