Pagini recente » Cod sursa (job #299297) | Cod sursa (job #2769306) | Cod sursa (job #2338449) | Cod sursa (job #29274) | Cod sursa (job #3157460)
#include <bits/stdc++.h>
using namespace std;
#define oo 0x3f3f3f3f
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m;
vector<int> graph[1005];
int t[1005], C[1005][1005], F[1005][1005];
bool viz[1005];
void read() {
f>>n>>m;
int x, y;
for(int i = 1;i <= m;++i) {
f>>x>>y;
f>>C[x][y];
graph[x].push_back(y);
graph[y].push_back(x);
}
}
bool bfs() {
memset(viz, false, sizeof(viz));
viz[1] = true;
queue<int> q;
q.push(1);
while(!q.empty()) {
int node = q.front();
q.pop();
if(node == n) {
continue;
}
for(const auto& ngh : graph[node]) {
if(viz[ngh] || C[node][ngh] == F[node][ngh]) {
continue;
}
viz[ngh] = true;
t[ngh] = node;
q.push(ngh);
}
}
return viz[n];
}
void solve() {
int result = 0;
for(;bfs();) {
for(const auto& node : graph[n]) {
if(!viz[node] || C[node][n] == F[node][n]) {
continue;
}
t[n] = node;
int flow = oo;
for(int no = n;no != 1;no = t[no]) {
flow = min(flow, C[t[no]][no] - F[t[no]][no]);
}
if(flow == oo) {
continue;
}
for(int no = n;no != 1;no = t[no]) {
F[t[no]][no] += flow;
F[no][t[no]] -= flow;
}
result += flow;
}
}
g<<result;
}
int main() {
read();
solve();
return 0;
}