Pagini recente » Cod sursa (job #143125) | Cod sursa (job #2599132) | Cod sursa (job #2283822) | Cod sursa (job #3229026) | Cod sursa (job #1754399)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int k,i,x,m,j,g[1005][1005],h1[1005][1005],ct,k1,i1,j1,m1;
vector<int>f[1005],h;
const long long inf=1e6;
bool bfs()
{
int k,i,m;
queue<int>q;
h=vector<int>(x+1,0);
for(q.push(1),h[1]=1;!q.empty();)
{
k=q.front(); q.pop();
m=f[k].size();
for(i=0;i<m;i++)
{
if(!h[f[k][i]]&&g[k][f[k][i]]>h1[k][f[k][i]])
{
h[f[k][i]]=k;
q.push(f[k][i]);
if(f[k][i]==x)
return 1;
}
}
}
return 0;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&x,&m);
for(k=1;k<=m;k++)
{
scanf("%d%d%d",&i,&j,&ct);
g[i][j]=ct;
f[i].push_back(j);
f[j].push_back(i);
}
for(;bfs();)
{
ct=inf;
for(k=x;k!=1;k=h[k])
ct=min(ct,(g[h[k]][k]-h1[h[k]][k]));
for(k=x;k!=1;k=h[k])
{
h1[k][h[k]]-=ct;
h1[h[k]][k]+=ct;
}
k1+=ct;
}
printf("%d\n",k1);
return 0;
}