Pagini recente » Cod sursa (job #2279390) | Cod sursa (job #2785403) | Cod sursa (job #2790403) | Cod sursa (job #2822941) | Cod sursa (job #1941272)
#include <fstream>
#include <queue>
using namespace std;
int noduri, muchii, x, y, c, fmax, maxflow, i, m[1001][1001], t[1001];
bool viz[1001];
queue<int>q;
void bfs (int nod) {
int nodc = 1;
q.push(nod);
while (!q.empty()) {
nodc = q.front(); q.pop(); viz[nodc] = true;
for (int i = 1; i <= noduri; i++)
if (m[nodc][i] > 0 and !viz[i])
q.push(i), t[i] = nodc;
}
}
int main () {
ifstream fi("maxflow.in");
ofstream fo("maxflow.out");
fi >> noduri >> muchii;
for (int i = 1; i <= muchii; i++)
fi >> x >> y >> c, m[x][y] = c;
while (true) {
for (int i = 1; i <= noduri; i++)
viz[i] = false;
bfs(1);
if (!viz[noduri])
break;
fmax = 2e9, i = noduri;
while (i != 1)
fmax = min(fmax, m[t[i]][i]), i = t[i];
i = noduri;
while (i != 1)
m[t[i]][i] -= fmax, m[i][t[i]] += fmax, i = t[i];
maxflow += fmax;
}
fo << maxflow;
return 0;
}