Pagini recente » Cod sursa (job #2113402) | Cod sursa (job #2617939) | Cod sursa (job #69643) | Cod sursa (job #78895) | Cod sursa (job #2005970)
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000
#define vec first
#define cap second
vector < pair <int, int> > V[MAXN + 1];
int lst[MAXN + 1];
bool viz[MAXN + 1];
int Matr[MAXN + 1][MAXN + 1];
int N, M, boosted = 1;
int find_path() {
queue < int > q;
int bst = INT_MAX;
q.push(1);
memset(viz, 0, sizeof viz);
memset(lst, 0, sizeof lst);
lst[1] = -1;
bool st = 1;
viz[1] = 1;
while(!q.empty() && st) {
int nod = q.front();
q.pop();
for(unsigned int i = 0;i < V[nod].size();i++) if(!viz[V[nod][i].vec] && V[nod][i].cap > 0) {
bst = min(bst, V[nod][i].cap);
lst[V[nod][i].vec] = nod;
viz[V[nod][i].vec] = 1;
if(V[nod][i].vec == N) {
st = 0;
break;
}
q.push(V[nod][i].vec);
}
}
boosted++;
if(bst == INT_MAX)
return 0;
for(int nod = N;nod != -1;) {
int aa = lst[nod];
if(aa == -1)
break;
V[aa][Matr[aa][nod]].cap -= bst;
V[nod][Matr[nod][aa]].cap += bst;
/* cerr << nod << " " << lst[nod] << " ";
cerr << V[aa][Matr[aa][nod]].cap << " " << V[nod][Matr[nod][aa]].cap << "\n"; */
nod = aa;
}
return bst;
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d%d", &N, &M);
for(int i = 1;i <= M;i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
V[x].push_back({y, z});
Matr[x][y] = V[x].size() - 1;
V[y].push_back({x, 0});
Matr[y][x] = V[y].size() - 1;
}
bool ok = 1;
int ans = 0;
while(ok) {
int PathVal = find_path();
if(PathVal == 0) {
break;
}
else
ans += PathVal;
}
printf("%d", ans);
return 0;
}