这就是以后我的板子啦~~~
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 444
int tot,next[N],first[N],w[N],v[N],n,m,ch[N];
void add(int from,int to,int weight){
v[tot]=to;w[tot]=weight;
next[tot]=first[from];
first[from]=tot++;
}
bool tell(){
memset(ch,-1,sizeof(ch));
queue<int>q;
q.push(1);ch[1]=0;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=first[t];~i;i=next[i])
if(w[i]&&ch[v[i]]==-1)
q.push(v[i]),ch[v[i]]=ch[t]+1;
}
return ch[n]!=-1;
}
int zeng(int a,int b){
if(a==n)return b;
int r=0;
for(int i=first[a];~i&&b>r;i=next[i])
if(ch[a]+1==ch[v[i]]&&w[i]){
int t=zeng(v[i],min(b-r,w[i]));
w[i]-=t;w[i^1]+=t;r+=t;
}
if(!r)ch[a]=-1;
return r;
}
int dinic(){
int ans=0,jy;
while(tell())while(jy=zeng(1,0x3fffffff))ans+=jy;
return ans;
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
memset(first,-1,sizeof(first));
register int xx,yy,zz;
tot=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);add(yy,xx,0);
}
printf("%d\n",dinic());
}
}
还有一中写在结构体里面的:
(假设1为源点,n为汇点)
struct Dinic{
int fst[N],next[N],w[N],v[N],vis[N],cnt;
void init(){memset(fst,-1,sizeof(fst)),cnt=0;}
void add(int x,int y,int z){
w[cnt]=z,v[cnt]=y;
next[cnt]=fst[x],fst[x]=cnt++;
}
bool tell(){
memset(vis,-1,sizeof(vis));
queue<int>q;
q.push(1),vis[1]=0;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=fst[t];~i;i=next[i])
if(w[i]&&vis[v[i]]==-1)
q.push(v[i]),vis[v[i]]=vis[t]+1;
}
return vis[n]!=-1;
}
int zeng(int x,int y){
if(x==n)return y;
int r=0;
for(int i=fst[x];~i&&y>r;i=next[i]){
if(w[i]&&vis[v[i]]==vis[x]+1){
int t=zeng(v[i],min(y-r,w[i]));
w[i]-=t,w[i^1]+=t,r+=t;
}
}
if(!r)vis[x]=-1;
return r;
}
void flow(){
int ans=0,xx;
while(tell())while(xx=zeng(1,0x3fffffff))ans+=xx;
printf("%d\n",ans);
}
}dinic;