Pagini recente » Cod sursa (job #275858) | Cod sursa (job #2175442) | Cod sursa (job #664789) | Cod sursa (job #2942228) | Cod sursa (job #1926783)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int NMax = 1e3 + 5;
vector < int > G[NMax];
bool used[NMax];
int D[NMax][NMax], C[NMax][NMax];
int father[NMax];
inline bool BFS(const int &n) {
deque < int > dq;
memset(used, false, sizeof(used));
used[1] = true;
dq.push_back(1);
while(!dq.empty()) {
int node = dq.front(); dq.pop_front();
for(auto const &it: G[node]) {
if(used[it] == false && D[node][it] != C[node][it]) {
used[it] = true;
father[it] = node;
dq.push_back(it);
if(it == n) return true;
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; i++) {
int a, b, c;
fin >> a >> b >> c;
D[a][b] += c;
G[a].push_back(b);
G[b].push_back(a);
}
int flow;
for(flow = 0; BFS(n);) {
for(auto const &it: G[n]) {
if(used[it] == true && D[it][n] != C[it][n]) {
father[n] = it;
int flowMin = INFINITY;
for(int node = n; node != 1; node = father[node]) {
flowMin = min(flowMin, D[father[node]][node] - C[father[node]][node]);
}
flow += flowMin;
for(int node = n; node != 1; node = father[node]) {
C[father[node]][node] += flowMin;
C[node][father[node]] -= flowMin;
}
}
}
}
fout << flow;
return 0;
}