Pagini recente » Cod sursa (job #1308184) | Cod sursa (job #1161883) | Cod sursa (job #2664245)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
const int maxn = 1005, maxm = 5005, inf = 1 << 30;
int n, m, x, y, z, ans;
vector <int> nod[maxn];
queue <int> q;
bool is[maxn];
int p[maxn];
int c[maxn][maxn], us[maxn][maxn];
bool bfs() {
memset(is, false, sizeof(is));
while(!q.empty()) {
q.pop();
}
q.push(1);
is[1] = true;
while(!q.empty()) {
int x = q.front();
q.pop();
if(x == n) { continue; }
for(auto u : nod[x]) {
if(!(is[u] == true || us[x][u] == c[x][u])) {
p[u] = x;
q.push(u);
is[u] = true;
}
}
}
return is[n];
}
int main()
{
int i;
f >> n >> m;
for(i = 1; i <= m; i ++) {
f >> x >> y >> z;
nod[x].push_back(y);
nod[y].push_back(x);
c[x][y] = z;
}
while(bfs()) {
for(auto u : nod[n]) {
if(us[u][n] == c[u][n] || is[u] == false) {
continue;
}
p[n] = u;
int minim = inf;
for(int nd = n; nd != 1; nd = p[nd]) {
minim = min(minim, c[p[nd]][nd] - us[p[nd]][nd]);
}
for(int nd = n; nd != 1; nd = p[nd]) {
us[p[nd]][nd] += minim;
us[nd][p[nd]] -= minim;
}
ans += minim;
}
}
g << ans << '\n';
f.close(); g.close();
return 0;
}