板子题
当前弧优化版本
目前效率最高
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double g=10.0,eps=1e-12;
const int N=200+10,maxn=10000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
struct edge{
int to,Next,c;
}e[maxn];
int s,t,cnt,head[N],cur[N];
int n,m;
void init()
{
cnt=0;
memset(head,-1,sizeof head);
}
void add(int u,int v,int c)
{
e[cnt].to=v;
e[cnt].c=c;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].to=u;
e[cnt].c=0;
e[cnt].Next=head[v];
head[v]=cnt++;
}
int dis[N];
bool bfs()
{
queue<int>q;
memset(dis,-1,sizeof dis);
dis[s]=1;
q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];~i;i=e[i].Next)
{
int y=e[i].to;
if(dis[y]==-1&&e[i].c>0)
{
dis[y]=dis[x]+1;
q.push(y);
}
}
}
return dis[t]!=-1;
}
ll dfs(int u,ll mx)
{
if(u==t)return mx;
ll flow=0,f;
for(int &i=cur[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(dis[x]==dis[u]+1&&e[i].c>0&&(f=dfs(x,min(mx-flow,1ll*e[i].c))))
{
e[i].c-=f;
e[i^1].c+=f;
flow+=f;
if(flow == mx) break;
}
}
if(flow==0)dis[u]=-2;
return flow;
}
ll maxflow()
{
ll ans=0,f;
while(bfs())
{
for(int i=0;i<=n;i++)cur[i]=head[i];
while((f=dfs(s,inf)))ans+=f;
}
return ans;
}
int main()
{
init();
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
printf("%lld
",maxflow());
return 0;
}