Pagini recente » Cod sursa (job #459849) | Profil dorin31 | Statistici Vlad Andrei Popescu (vapopescu) | Cod sursa (job #1356056) | Cod sursa (job #1168944)
#include <cstdio>
#include <algorithm>
#include <vector>
#define Nmax 1005
#define INF 2000000000
using namespace std;
int N,C[Nmax][Nmax],F[Nmax][Nmax],tata[Nmax],Q[Nmax];
bool viz[Nmax];
vector <int> L[Nmax];
inline bool Bfs()
{
int i,nod,n,len,st,dr;
for(i=2;i<=N;++i)
viz[i]=false;
viz[1]=true;
st=dr=Q[1]=1;
while(st<=dr)
{
nod=Q[st++];
if(nod==N)
continue;
len=L[nod].size();
for(i=0;i<len;++i)
{
n=L[nod][i];
if(!viz[n] && F[nod][n]<C[nod][n])
{
viz[n]=true;
Q[++dr]=n;
tata[n]=nod;
}
}
}
return viz[N];
}
int main()
{
int M,x,y,z,flow=0,minflow,len,i,n;
freopen ("maxflow.in","r",stdin);
freopen ("maxflow.out","w",stdout);
scanf("%d%d", &N,&M);
while(M--)
{
scanf("%d%d%d", &x,&y,&z);
L[x].push_back(y);
L[y].push_back(x);
C[x][y]+=z;
}
while(Bfs())
{
len=L[N].size();
for(i=0;i<len;++i)
{
n=L[N][i];
if(viz[n] && C[n][N]>F[n][N])
{
tata[N]=n;
minflow=INF;
for(n=N;tata[n];n=tata[n])
minflow=min(minflow,C[tata[n]][n]-F[tata[n]][n]);
if(!minflow)
continue;
for(n=N;tata[n];n=tata[n])
{
F[tata[n]][n]+=minflow;
F[n][tata[n]]-=minflow;
}
flow+=minflow;
}
}
}
printf("%d\n", flow);
return 0;
}