Pagini recente » Cod sursa (job #2713440) | Cod sursa (job #1462318) | Cod sursa (job #2000403) | Cod sursa (job #2040636) | Cod sursa (job #2787869)
#include <bits/stdc++.h>
using namespace std;
inline void Open(const string Name) {
#ifndef ONLINE_JUDGE
(void)!freopen((Name + ".in").c_str(), "r", stdin);
(void)!freopen((Name + ".out").c_str(), "w", stdout);
#endif
}
const int INF = 2e9;
vector <int> Gr[1001];
int flow[1001][1001], capacity[1001][1001];
int depth[1001];
int N, M, x, y, z;
bool bfs() {
queue <int> q;
memset(depth, 0, sizeof(depth));
depth[1] = 1;
q.push(1);
while(!q.empty()) {
int nod = q.front();
q.pop();
for(int &v : Gr[nod]) {
if(depth[v] == 0 && capacity[nod][v] > flow[nod][v]) {
depth[v] = depth[nod] + 1;
q.push(v);
}
}
}
return (depth[N] != 0);
}
int dfs(int nod, int fflow) {
if(nod == N)
return fflow;
for(int &v : Gr[nod]) {
if(depth[v] == depth[nod] + 1 && capacity[nod][v] - flow[nod][v] > 0) {
int tmpFlow = dfs(v, min(fflow, capacity[nod][v] - flow[nod][v]));
if(tmpFlow > 0) {
flow[nod][v] += tmpFlow;
flow[v][nod] -= tmpFlow;
return tmpFlow;
}
}
}
depth[nod] = 0;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Open("maxflow");
cin >> N >> M;
for(int i = 1;i <= M;i++) {
cin >> x >> y >> z;
Gr[x].emplace_back(y);
Gr[y].emplace_back(x);
capacity[x][y] = z;
}
int maxFlow = 0;
while(bfs()) {
int tmpFlow = 0;
do {
tmpFlow = dfs(1, INF);
maxFlow += tmpFlow;
}while(tmpFlow > 0);
}
cout << maxFlow;
return 0;
}