Pagini recente » Cod sursa (job #550727) | Cod sursa (job #557503) | Cod sursa (job #1100272) | Cod sursa (job #2093488) | Cod sursa (job #1892459)
#include <fstream>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int cap[1005][1005], flx[1005][1005], x, y, i, n, m, c, l;
bool viz[1005];
int wh[1005], sol;
vector<int>ls[1005];
bool ok;
void df() {
queue<int> q;
memset(viz, 0,sizeof(viz));
q.push(1);
viz[1]=1;
while (q.empty() == 0) {
x = q.front();
q.pop();
if (x==n)
continue;
l = ls[x].size();
for (i = 0; i < l; i++) {
y = ls[x][i];
if (cap[x][y] - flx[x][y] > 0 && viz[y] ==0) {
viz[y] = 1;
wh[y] = x;
q.push(y);
}
}
}
}
int main() {
f >> n >> m;
while (m--) {
f >> x >> y >> c;
ls[x].push_back(y);
ls[y].push_back(x);
cap[x][y] = c;
//cap[y][x] = -c;
}
ok = 1;
while (1) {
df();
//cout<<viz[n];
if (viz[n] == 0)
break;
for (i = 0; i < ls[n].size(); i++) {
y = ls[n][i];
if (cap[y][n]-flx[y][x]== 0)
continue;
wh[n]=y;
//if ()
int minim = 100000005;
y = n;
while (y>1) {
if (cap[wh[y]][y]-flx[wh[y]][y] <= 0)
continue;
minim = min(minim,cap[wh[y]][y]-flx[wh[y]][y]);
y = wh[y];
}
sol += minim;
y = n;
while (y>1) {
flx[wh[y]][y] += minim;
flx[y][wh[y]] -= minim;
y = wh[y];
}
}
}
g << sol;
}