Pagini recente » Cod sursa (job #338672) | Cod sursa (job #2259602) | Cod sursa (job #422195) | Cod sursa (job #2184292) | Cod sursa (job #2873075)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
#define DIM 1000
#define INF (1LL << 30)
int n, m, x, y, cost, flux;
int c[DIM + 1][DIM + 1], f[DIM + 1][DIM + 1], t[DIM + 1];
static inline bool bfs(int s, int d) {
queue <int> Q;
for(int i = 1; i <= n; i++)
t[i] = 0;
t[s] = -1;
Q.push(s);
while(!Q.empty()) {
int nod = Q.front();
Q.pop();
for(int i = 1; i <= n; i++)
if(!t[i] && c[nod][i] > f[nod][i]) {
Q.push(i);
t[i] = nod;
if(i == d)
return 1;
}
}
return 0;
}
static inline void Flux_maxim(int s, int d) {
int r, i;
for(flux = 0; bfs(s, d); flux += r) {
r = INF;
i = d;
while(i != s) {
r = min(r, c[t[i]][i] - f[t[i]][i]);
i = t[i];
}
i = d;
while(i != s) {
f[t[i]][i] += r;
f[i][t[i]] -= r;
i = t[i];
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++) {
fin >> x >> y >> cost;
c[x][y] = cost;
}
Flux_maxim(1, n);
fout << flux;
return 0;
}