#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 210;
const int maxm = 500;
const int INF = 0x7FFFFFF;
#define mem(name,value) memset((name),(value),sizeof(name))
struct Side{
int to,next,c;
}side[maxm];
int top,node[maxn];
void add_side(int u,int v,int c,int rc){
side[top]=(Side){v,node[u],c};node[u]=top++;
side[top]=(Side){u,node[v],rc};node[v]=top++;
}
int start,end,cnt,dis[maxn],gap[maxn];
int get_flow(int u,int flow){
//printf("%d %d\n",u,flow);
if(u==end)return flow;
int ans=0;
for(int i=node[u];i!=-1;i=side[i].next){
int v=side[i].to,c=side[i].c;
if(dis[u]>dis[v]&&c){
int f=get_flow(v,min(flow-ans,c));
ans+=f;
side[i].c-=f;
side[i^1].c+=f;
if(ans==flow)return ans;
}
}
if(!(--gap[dis[u]]))dis[start]=cnt+2;
gap[++dis[u]]++;
return ans;
}
int main(){
int n,m;
while(~scanf("%d%d",&m,&n)){
top=0; //一定记得要初始化
mem(node,-1);
mem(gap,0);
mem(dis,0);
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_side(u,v,w,0);
}
int ans=0;
start=1; //起始点编号
end=n; //汇点编号
cnt=n; //节点总数(包括源点汇点)
gap[0]=cnt;
while(dis[start]<cnt)ans+=get_flow(start,INF);
printf("%d\n",ans); //返回最大流
}
}
最大流ISAP模板
最新推荐文章于 2020-11-17 22:24:02 发布