Pagini recente » Cod sursa (job #2493501) | Cod sursa (job #232062) | Monitorul de evaluare | Cod sursa (job #2513587) | Cod sursa (job #1233611)
#include <cstdio>
#include <deque>
#include <vector>
#include <cstring>
#include <algorithm>
#define N 1010
#define oo 300000000
using namespace std;
vector<int>v[N];
deque<int>Q;
int tata[N],x,n,m,y,z,c[N][N],fl[N][N],flow,sol;
bool bf()
{
Q.clear();Q.push_back(1);
memset(tata,0,sizeof(tata));
while(Q.size())
{
x=Q.front();
if(x==n)return 1;
for(auto it:v[x])
if(!tata[it]&&c[x][it]-fl[x][it]>0)
{
tata[it]=x;
Q.push_back(it);
}
Q.pop_front();
}
return 0;
}
void upd()
{
for(auto it:v[n])
if(tata[it]&&c[it][n]-fl[it][n]>0)
{
flow=oo;
for(x=it,y=n;y!=1;y=x,x=tata[x])
flow=min(flow,c[x][y]-fl[x][y]);
for(x=it,y=n;y!=1;y=x,x=tata[x])
{
fl[x][y]+=flow;
fl[y][x]-=flow;
}
sol+=flow;
}
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&n,&m);
for(;m;m--)
{
scanf("%d%d%d",&x,&y,&z);
v[x].push_back(y);
v[y].push_back(x);
c[x][y]=z;
}
while(bf())
upd();
printf("%d\n",sol);
return 0;
}