Pagini recente » Cod sursa (job #1326397) | Cod sursa (job #686376) | Cod sursa (job #2624850) | Cod sursa (job #1485090) | Cod sursa (job #526029)
Cod sursa(job #526029)
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1005;
int n, m, S, D;
const int INF = 0x3f3f3f3f;
int c[N][N], f[N][N], t[N];
vector<int> a[N];
int BFS() {
int i, j, k;
int cd[1005], v[N];
for(i = 1; i <= n; ++i)
t[i] = v[i] = 0;
cd[0] = 1;
cd[1] = S;
v[1] = 1;
for(i = 1; i <= cd[0]; ++i) {
k = cd[i];
for(j = 0; j < a[k].size(); ++j)
if(f[k][a[k][j]] < c[k][a[k][j]] && !v[a[k][j]]) {
cd[++cd[0]] = a[k][j];
t[a[k][j]] = k;
v[a[k][j]] = 1;
}
}
return v[D];
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int i, j, C, x, y, s[N], d[N];
scanf("%d %d", &n, &m);
for(i = 1; i <= m; ++i)
scanf("%d %d %d", &x, &y, &C), c[x][y] = C, a[x].push_back(y), a[y].push_back(x), s[x] = 1, d[y] =1;
int flux = 0, r = 0;
for(i = 1; i <= n; ++i)
if(d[i] == 0) {
S = i;
break;
}
for(i = 1; i <= n; ++i)
if(s[i] == 0) {
D = i;
break;
}
for(int k = 1;BFS() ; ++k) {
for(j = 0; j < a[D].size(); ++j){
if( f[a[D][j]][D] < c[a[D][j]][D]){
i = D;
t[D] = a[D][j];
r = INF;
while(t[i]) {
r = min(r, c[t[i]][i] - f[t[i]][i]);
i = t[i];
}
// printf("%d\n", r);
i = D;
while(t[i]) {
f[i][t[i]] -= r;
f[t[i]][i] += r;
i = t[i];
}
flux += r;
}
}
}
printf("%d\n", flux);
return 0;
}