#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int x,y,c,next,other;
};
node a[2110000];
int len = 0,last[5000],st,ed;
int list[5000];
queue <int> qu;
void add(int x,int y,int w)
{
int k1,k2;
a[++len].next = last[x];
k1 = len;
a[len].x = x;
a[len].y = y;
a[len].c = w;
last[x] = len;
a[++len].next = last[y];
k2 = len;
a[len].x = y;
a[len].y = x;
a[len].c = 0;
last[y] = len;
a[k1].other = k2;
a[k2].other = k1;
}
int h[5000];
bool bfs()
{
memset(h,0,sizeof(h));
h[st] = 1;
int head,tail;
list[1] = st;
head = 1;
tail = 2;
while(head != tail)
{
int x = list[head];
// cout<<x<<endl;
for(int k = last[x];k;k = a[k].next)
{
int y = a[k].y;
// cout<<k<<" "<<a[k].c<<" "<<y<<endl;
if(a[k].c > 0 && h[y] == 0)
{
h[y] = h[x] + 1;
list[tail++] = y;
// cout<<y<<endl;
}
}
head++;
}
/*for(int i = 1;i <= ed;i++)
cout<<h[i]<<" ";
cout<<endl;
for(int i = 1;i <= len;i++)
cout<<a[i].c<<" ";
cout<<endl;*/
if(h[ed] > 0)
return true;
else
return false;
}
int mymin(int x,int y){return x < y ? x : y;}
int find(int x,int f)
{
if(x == ed)
return f;
int s = 0,t;
for(int k = last[x];k;k = a[k].next)
{
int y = a[k].y;
if(s < f && h[y] == (h[x] + 1) && a[k].c > 0)
{
// cout<<a[k].c<<" "<<f - s<<endl;
t = find(y,mymin(a[k].c,f - s));
s += t;
// cout<<t<<endl;
a[k].c -= t;
a[a[k].other].c += t;
// cout<<k<<"_"<<a[k].c<<" "<<a[k].other<<"_"<<a[a[k].other].c<<endl;
}
}
if(s == 0)
h[x] = 0;
return s;
}
int main()
{
int n,m;
cin>>m>>n;
st = 1;
ed = n;
len = 0;
memset(last,0,sizeof(last));
for(int i = 1;i <= m;i++)
{
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
}
// cout<<endl;
int s = 0,t;int l = 0;
while(bfs() == true)
{
s += find(st,99999999);
if(l > 10)
break;
l++;
}
cout<<s;
return 0;
}
/*
4 4
1 2 10
2 4 5
2 3 20
3 4 10
*/
原文:https://www.cnblogs.com/DukeLv/p/9112734.html