Pagini recente » Cod sursa (job #1247794) | Cod sursa (job #726607) | Cod sursa (job #2931672) | Borderou de evaluare (job #1567503) | Cod sursa (job #2133787)
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int A[1010][1010];
int n,m;
int rez;
bitset <1010> B;
map <int,int> M;
queue <int> Q;
bool bfs(int x)
{
Q.push(x);
M[x]=2000;
B[x]=1;
int mini=200000;
bool ok=0;
while (!Q.empty())
{
int cr=Q.front();
Q.pop();
for (int i=1; i<=n; i++)
{
if (A[cr][i]!=0 && B[i]==0)
{
M[i]=cr;
B[i]=1;
if (i==n)
{
ok=1;
break;
}
Q.push(i);
}
}
if (ok==1)
break;
}
if (ok==0)
return 0;
int pz=n;
while (pz!=1)
{
mini=min(mini,A[M[pz]][pz]);
pz=M[pz];
}
pz=n;
rez+=mini;
while (pz!=1)
{
A[M[pz]][pz]-=mini;
A[pz][M[pz]]+=mini;
pz=M[pz];
}
B.reset();
M.clear();
while (!Q.empty())
Q.pop();
return 1;
}
int main()
{
fin>>n>>m;
for (int i=1; i<=m; i++)
{
int x,y,z;
fin>>x>>y>>z;
A[x][y]=z;
}
while (bfs(1));
fout<<rez;
return 0;
}