Pagini recente » Cod sursa (job #57817) | Cod sursa (job #2249437) | Cod sursa (job #820365) | Cod sursa (job #167754) | Cod sursa (job #687155)
Cod sursa(job #687155)
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define maxN 1005
#define PB push_back
#define INF 0x3f3f3f3f
int N , M , flux[maxN][maxN] , cap[maxN][maxN] , tata[maxN] , sol;
bool viz[maxN];
vector <int> lista[maxN];
queue <int> Q;
int poti_pompa ()
{
Q.push (1);
memset (viz , 0 , sizeof (viz));
viz[1] = true;
while (!Q.empty ())
{
int nod = Q.front ();
Q.pop ();
for (unsigned int i = 0 ; i < lista[nod].size () ; ++i)
{
int nodcur = lista[nod][i];
if (nodcur == N)
break;
if (viz[nodcur])
continue;
if (cap[nod][nodcur] - flux[nod][nodcur] == 0)
continue;
tata[nodcur] = nod;
Q.push (nodcur);
}
}
if (!viz[N])
return 0;
int nod = N , minim = INF;
while (nod != 1)
{
minim = min (minim , cap[nod][tata[nod]] - flux[nod][tata[nod]]);
nod = tata[nod];
}
nod = N;
while (nod != 1)
{
flux[tata[nod]][nod] += minim;
flux[nod][tata[nod]] -= minim;
nod = tata[nod];
}
sol += minim;
return 1;
}
int main ()
{
freopen ("maxflow.in" , "r" , stdin);
freopen ("maxflow.out" , "w" , stdout);
scanf ("%d %d" , &N , &M);
int x , y , z;
for (int i = 1 ; i <= M ; ++i)
{
scanf ("%d %d %d" , &x , &y , &z);
lista[x].PB (y);
lista[y].PB (x);
cap[x][y] = z;
}
while (poti_pompa());
printf ("%d" , sol);
return 0;
}