Pagini recente » Cod sursa (job #1370225) | Cod sursa (job #3144778) | Cod sursa (job #528190) | Cod sursa (job #2700777) | Cod sursa (job #2133962)
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
map <int,int> A[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 (map<int,int>::iterator it=A[cr].begin();it!=A[cr].end();it++)
{
if (it->second!=0 && B[it->first]==0)
{
M[it->first]=cr;
B[it->first]=1;
if (it->first==n)
{
ok=1;
break;
}
Q.push(it->first);
}
}
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;
if (A[M[pz]][pz]==0)
A[M[pz]].erase(A[M[pz]].find(pz));
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;
}