Pagini recente » Cod sursa (job #1713358) | Cod sursa (job #2443650) | Cod sursa (job #1133077) | Cod sursa (job #1190735) | Cod sursa (job #945984)
Cod sursa(job #945984)
#include <fstream>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define in "maxflow.in"
#define out "maxflow.out"
#define N 1005
#define f(x,y) F[x][y]
#define c(x,y) C[x][y]
#define oo 1 << 30
#define S 1
#define D n
typedef unsigned u;
vector <short> graf[N], tata (N, 0);
queue <short> Q;
int C[N][N], F[N][N];
int n, m, sol;
void BFS () {
Q.push (S);
while (Q.size()) {
int x = Q.front();
Q.pop();
for (u i = 0; i < graf[x].size(); ++i)
if (!tata[graf[x][i]] && graf[x][i] != S) {
if (c (x, graf[x][i]) - f (x, graf[x][i]) > 0) {
Q.push (graf[x][i]);
tata[graf[x][i]] = x;
}
else
if (f (graf[x][i], x) > 0) {
Q.push (graf[x][i]);
tata[graf[x][i]] = -x;
}
}
}
}
int main () {
ifstream fin (in);
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y, c;
fin >> x >> y >> c;
c (x, y) = c;
graf[x].push_back (y);
graf[y].push_back (x);
}
fin.close();
do {
for (int i = 1; i <= n; ++i)
tata[i] = 0;
BFS ();
if (!tata[D]) //Iesirea retelei nu a fost marcata
break;
short x = D;
int MIN = oo;
while (tata[x]) { //Aflarea minimului nesaturat
if (tata[x] > 0)
MIN = min (MIN, c (tata[x], x) - f (tata[x], x));
else
MIN = min (MIN, f (x, -tata[x]));
x = abs(tata[x]);
}
sol += MIN;
x = D;
while (tata[x]) { // Ameliorarea fluxlui
if (tata[x] > 0)
f (tata[x], x) += MIN;
else
f (x, -tata[x]) -= MIN;
x = abs(tata[x]);
}
} while (1);
ofstream fout (out);
fout << sol;
fout.close();
return 0;
}