网络流板子
虽然是yxs学长的板子,但看起来很帅就拿过来用啦~
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
namespace EMT{
typedef long long ll;typedef double db;//(double)clock() / (double)CLOCKS_PER_SEC;
#define pf printf
#define F(i,a,b) for(register int i=a;i<=b;i++)
#define D(i,a,b) for(register int i=a;i>=b;i--)
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
inline void file(){freopen("in.in","r",stdin);freopen("my.out","w",stdout);}
inline int max(int a,int b){return a>b?a:b;}inline ll min(ll a,ll b){return a<b?a:b;}
inline void pi(ll x){pf("%lld ",x);}inline void pn(){pf("\n");}
const int N=207,M=10007;int head[N],co=1;const ll inf=1e18,maxn=0x3f3f3f3f;
struct node{int next,to,w;}e[M];
inline void add(int next,int to,int w){e[++co].next=head[next],e[co].to=to,e[co].w=w,head[next]=co;}
int n,m,s,t,Hd[N],dis[N],q[N],c[N],hd,tail;ll ans;
inline bool bfs(){
F(i,1,n)dis[i]=maxn,head[i]=Hd[i];
dis[s]=0;q[hd=tail=1]=s;
while(hd<=tail){
int x=q[hd++];
for(register int i=head[x];i;i=e[i].next){
if(e[i].w){
if(dis[e[i].to]>dis[x]+1){
dis[e[i].to]=dis[x]+1;
q[++tail]=e[i].to;
}
}
}
if(x==t)return 1;
}return 0;
}
inline ll dfs(int s,ll flow){
if(s==t)return flow;
ll rest=flow,go;
for(register int i=head[s];i;head[s]=i=e[i].next){
if(e[i].w){
if(dis[e[i].to]==dis[s]+1){
go=dfs(e[i].to,min(rest,e[i].w));
if(go)e[i].w-=go,e[i^1].w+=go,rest-=go;
else dis[e[i].to]=0;
}
}
if(!rest)break;
}
return flow-rest;
}
inline short main(){
n=read(),m=read(),s=read(),t=read();
F(i,1,m){
int x=read(),y=read(),z=read();
add(x,y,z);add(y,x,0);
}
F(i,1,n)Hd[i]=head[i];
while(bfs())ans+=dfs(s,inf);
pi(ans);
return 0;
}
}
signed main(){return EMT::main();}
Everything that kills me makes me feel alive.