Pagini recente » Cod sursa (job #11489) | Cod sursa (job #520827) | Cod sursa (job #1840660) | Cod sursa (job #181336) | Cod sursa (job #1914048)
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#include <iostream>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
const int maxn=1005;
const int INF=1<<30;
int C[maxn][maxn];
int F[maxn][maxn];
int TT[maxn];
vector<int> G[maxn];
int N, M;
int cd[maxn];
int viz[maxn];
int BF()
{
int i, j, nod, next;
queue <int> Q;
Q.push(1);
memset(viz, 0, sizeof(viz));
viz[1] = 1;
while (!Q.empty())
{
nod = Q.front();
if (nod == N) {Q.pop();continue;}
for (j=0; j<G[nod].size(); j++)
{
next = G[nod][j];
if (C[nod][next] == F[nod][next] || viz[next]==1) continue;
viz[next]=1;
Q.push(next);
TT[next] = nod;
}
Q.pop();
}
return viz[N];
}
int main()
{
int i, flow, fmin, x, y, z, nod;
f>>N>>M;
for (i = 1; i <= M; i++)
{
f>>x>>y>>z;
C[x][y] += z;
G[x].push_back(y);
G[y].push_back(x);
}
flow=0;
while (BF()!=0)
{
for (i = 0; i < G[N].size(); i++)
{
nod = G[N][i];
if (F[nod][N] == C[nod][N] || viz[nod]==0) continue;
TT[N] = nod;
fmin = INF;
for (nod = N; nod != 1; nod = TT[nod])
fmin = min(fmin, C[ TT[nod] ][nod] - F[ TT[nod] ][nod]);
if (fmin == 0) continue;
for (nod = N; nod != 1; nod = TT[nod])
F[ TT[nod] ][nod] += fmin;
flow += fmin;
}
}
g<<flow;
return 0;
}