1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #define LL long long #define N 10010 #define M 200010 using namespace std;
int n,m,S,T; //n,m,s,t,分别表示点的个数、有向边的个数、源点序号、汇点序号 struct edge{LL v,c,ne;}e[M]; int h[N],idx=1; //从2,3开始配对 int d[N],cur[N];
void add(int a,int b,int c){ e[++idx]={b,c,h[a]}; h[a]=idx; } bool bfs(){ //对点分层,找增广路 memset(d,0,sizeof d); queue<int>q; q.push(S); d[S]=1; while(q.size()){ int u=q.front(); q.pop(); for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(d[v]==0 && e[i].c){ d[v]=d[u]+1; q.push(v); if(v==T)return true; } } } return false; } LL dfs(int u, LL mf){ //多路增广 if(u==T) return mf; LL sum=0; for(int i=cur[u];i;i=e[i].ne){ cur[u]=i; //当前弧优化 int v=e[i].v; if(d[v]==d[u]+1 && e[i].c){ LL f=dfs(v,min(mf,e[i].c)); e[i].c-=f; e[i^1].c+=f; //更新残留网 sum+=f; //累加u的流出流量 mf-=f; //减少u的剩余流量 if(mf==0)break;//余量优化 } } if(sum==0) d[u]=0; //残枝优化 return sum; } LL dinic(){ //累加可行流 LL flow=0; while(bfs()){ memcpy(cur, h, sizeof h); flow+=dfs(S,1e9); } return flow; } int main(){ int a,b,c; scanf("%d%d%d%d",&n,&m,&S,&T); while(m -- ){ scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,0); } printf("%lld\n",dinic()); return 0; }
|