Pagini recente » Cod sursa (job #1124777) | Cod sursa (job #1660256) | Cod sursa (job #1683830) | Cod sursa (job #1895754) | Cod sursa (job #2367882)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define f first
#define s second
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
vector <int > v[1010];
queue <pair<int,int> > q;
pair <int,int> p;
int t[1010],N,M,a[1010][1010],b[1010][1010],viz[1010],n,S;
int main()
{
int x,y,z,i,ok;
fin>>N>>M;
for(i=1; i<=M; i++)
{
fin>>x>>y>>z;
v[x].push_back(y);
a[x][y]=z;
}
ok=1;
while(ok==1)
{
ok=0;
q.push(make_pair(1,9999999999));
viz[1]=1;
while(!q.empty())
{
p=q.front();
q.pop();
if(p.f==N)
{
ok=1;
n=p.f;
S+=p.s;
while(n!=1)
{
b[t[n]][n]+=p.s;
n=t[n];
}
}
else
{
for(auto it:v[p.f])
{
if(viz[it]==0&&b[p.f][it]<a[p.f][it])
{
viz[it]=1;
q.push(make_pair(it,min(p.s,a[p.f][it]-b[p.f][it])));
t[it]=p.f;
}
}
}
}
if(ok==1)
{
for(i=1;i<=N;i++)
{
t[i]=0;
viz[i]=0;
}
}
}
fout<<S;
}