Pagini recente » Cod sursa (job #80549) | Cod sursa (job #2236225) | Cod sursa (job #2020908) | Cod sursa (job #1580302) | Cod sursa (job #1366099)
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
int cap[1010][1010];
int father[1010];
bool vis[1010];
vector<int> nodes[1010];
queue<int> q;
int n, m, le_max_flow;
void bfs()
{
bool ok = false;
memset(vis, 1, sizeof(vis));
q.push(1);
vis[1] = 0;
while(!q.empty()) {
int ind = q.front();
q.pop();
for(int next : nodes[ind]) {
if(vis[next] && (cap[ind][next] > 0)) {
q.push(next);
vis[next] = 0;
father[next] = ind;
}
}
}
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 0 ; i < m ; ++i) {
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
nodes[x].push_back(y);
nodes[y].push_back(x);
cap[x][y] = val;
}
for(bfs(); !vis[n] ; bfs()) {
for(int ind : nodes[n]) {
if(vis[ind] || cap[ind][n] <= 0) continue;
int max_flow = cap[ind][n];
for(int i = ind ; i != 1 ; i = father[i])
max_flow = max_flow > cap[father[i]][i] ? cap[father[i]][i] : max_flow;
cap[ind][n] -= max_flow;
cap[n][ind] += max_flow;
for(int i = ind ; i != 1 ; i = father[i]) {
cap[father[i]][i] -= max_flow;
cap[i][father[i]] += max_flow;
}
le_max_flow += max_flow;
}
}
printf("%d\n", le_max_flow);
return 0;
}