Pagini recente » Cod sursa (job #1445474) | Cod sursa (job #625559) | Cod sursa (job #1500689) | Cod sursa (job #2167301) | Cod sursa (job #2924460)
#include <bits/stdc++.h>
#define minn 1000000
using namespace std;
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
int n, m;
struct el
{
int nod;
int ramas;
int tip;
};
vector <el> v[10005];
int bagat;
int ok;
queue <pair<int , int>> Q;
int ordine[10005];
int rez;
int scad;
void inapoi(int nod,int mini)
{
if(ordine[nod]==1)
{
bagat+=mini;
scad=mini;
return;
}
else
{
for(int x=0; x<v[nod].size(); ++x)
{
int fiu=v[nod][x].nod;
if(ordine[fiu]+1==ordine[nod])
{
for(int y=0; y<v[fiu].size(); ++y)
if(v[fiu][y].nod==nod && v[fiu][y].ramas>0)
{
inapoi(fiu, min(mini , v[fiu][y].ramas));
v[fiu][y].ramas-=scad;
v[nod][x].ramas+=scad;
return;
}
}
}
}
}
void gasit()
{
Q.push({1,minn});
ordine[1]=1;
while(!Q.empty())
{
int nod=Q.front().first;
int cc=Q.front().second;
if(nod==n)
{
ok=1;
rez=0;
inapoi(n,minn);
for(int i=1; i<=n; ++i)
ordine[i]=0;
return;
}
for(int x=0; x<v[nod].size(); ++x)
{
int fiu=v[nod][x].nod;
int ram=v[nod][x].ramas;
if(ordine[fiu]==0 && ram>0)
{
ordine[fiu]=ordine[nod]+1;
Q.push({fiu , min(ram , cc)});
}
}
Q.pop();
}
}
int main()
{
f>>n>>m;
for(int i=1; i<=m; ++i)
{
int x, y, c;
f>>x>>y>>c;
v[x].push_back({y,c,1});
v[y].push_back({x,0,-1});
}
ok=1;
while(ok==1)
{
ok=0;
gasit();
for(int i=1; i<=n; ++i)
ordine[i]=0;
while(!Q.empty())
Q.pop();
}
g<<bagat;
return 0;
}