Pagini recente » Cod sursa (job #445617) | Cod sursa (job #2313196) | Cod sursa (job #1409166) | Cod sursa (job #946928) | Cod sursa (job #1626631)
#include <fstream>
#include <list>
#include <queue>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m, x, y, c, i, crt, mini, flow;
int flowMat[1010][1010], arbore[1010];
bool ok = true;
list<int> graf[1010];
queue<int> Q;
void makeArb() {
for (int i = 1 ; i <= n ; i++) arbore[i] = 0;
Q.push(1);
while (!Q.empty()) {
crt = Q.front(); Q.pop();
for (list<int>::iterator it = graf[crt].begin() ; it != graf[crt].end() ; it++) {
if (arbore[*it] == 0 && flowMat[crt][*it] > 0 && *it != n && *it != 1) {
arbore[*it] = crt;
Q.push(*it);
}
}
}
}
int getMin(int nod) {
int ans = flowMat[nod][n];
while (arbore[nod] != 0) {
ans = min(ans, flowMat[ arbore[ nod ] ][ nod ]);
nod = arbore[nod];
}
return ans;
}
void saturate(int nod, int val) {
flowMat[i][nod] -= val;
while (arbore[nod] != 0) {
flowMat[ arbore[ nod ] ][ nod ] -= val;
flowMat[ nod ][ arbore[ nod ] ] += val;
nod = arbore[nod];
}
}
int main()
{
fin>>n>>m;
for (i = 1 ; i <= m ; i++) {
fin>>x>>y>>c;
graf[x].push_back(y);
graf[y].push_back(x);
flowMat[x][y] = c;
}
while(ok) {
ok = false;
makeArb();
for (i = 2 ; i < n ; i++) {
if (arbore[i] != 0 && flowMat[i][n] > 0) {
ok = true;
mini = getMin(i);
flow += mini;
saturate(i, mini);
}
}
}
fout<<flow;
return 0;
}