/* 最大流EK算法,O(V*E*E) */ #include<stdio.h> #include<string.h> #include<queue> const int N = 205; int n,m; int cap[N][N],f[N][N],pre[N],rest[N]; int sNode,eNode; void init(){ memset(f,0,sizeof(f)); memset(cap,0,sizeof(cap)); } bool searchPath(){//找一条增广路 bool vist[N]={0}; queue<int>q; int u,v; u=sNode; vist[u]=1; pre[u]=u; rest[u]=1<<30; q.push(u); while(!q.empty()){ u=q.front(); q.pop(); for(v=1; v<=n; v++) if(!vist[v]&&cap[u][v]-f[u][v]>0) { vist[v]=1; pre[v]=u; if(cap[u][v]-f[u][v]>rest[u]) rest[v]=rest[u]; else rest[v]=cap[u][v]-f[u][v]; if(v==eNode) return true; q.push(v); } } return false; } int maxflow(){ int ans=0; while(searchPath()){ ans+=rest[eNode]; int v=eNode; while(v!=sNode){ int u=pre[v]; f[u][v]+=rest[eNode]; f[v][u]-=rest[eNode];//给一个回流的机会 v=u; } } return ans; } int main(){ int a,b,c; while(~scanf("%d%d",&n,&m)){ scanf("%d%d",&sNode,&eNode); init(); while(m--){ scanf("%d%d%d",&a,&b,&c); cap[a][b]+=c; } printf("%d\n",maxflow()) } }
时间: 2024-11-09 04:52:31