Pagini recente » Cod sursa (job #535686) | Cod sursa (job #2268723) | Cod sursa (job #2838391) | Cod sursa (job #1886380) | Cod sursa (job #2914072)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define EFC cin.tie(nullptr)->ios_base::sync_with_stdio(false)
int d1[5] = { 0, -1, 0, 1, 0 };
int d2[5] = { 0, 0, 1, 0, -1 };
const int mod = 666013;
int d11[9] = { 0 , -1, -1, 0, 1, 1, 1, 0, -1 };
int d22[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
int est[3] = { 0, -1, 0 };
int est1[3] = { 0, 0, 1 };
ifstream fin("abq.in");
ofstream fout("abq.out");
//----------------------------------
int n, m, C[1001][1001], s, t, p[1001], f[1001][1001];
vector<vector<int>> adj(1001);
struct MaxFLow {
void read() {
int x, y, cost;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> cost;
adj[x].push_back(y);
adj[y].push_back(x);
C[x][y] = cost;
}
s = 1;
t = n;
}
bool bfs() {
for (int i = 1; i <= n; i++) {
p[i] = -1;
}
p[s] = 0;
queue<int> q;
q.push(1);
while (!q.empty()) {
int nod = q.front();
q.pop();
if (nod == n) {
continue;
}
for (auto i : adj[nod]) {
if (p[i] == -1 && f[nod][i] < C[nod][i]) {
p[i] = nod;
q.push(i);
}
}
}
if (p[t] < 0) {
return 0;
}
return 1;
}
void pump() {
int max_flow = 0;
while (bfs()) {
for (auto i : adj[t]) {
int nod = i;
if (f[nod][t] == C[nod][t] || p[nod] == -1) {
continue;
}
p[t] = nod;
int minim = INT_MAX;
for (int j = t; j != 1; j = p[j])
minim = min(minim, C[p[j]][j] - f[p[j]][j]);
for (int j = t; j != 1; j = p[j]) {
f[p[j]][j] += minim;
f[j][p[j]] -= minim;
}
max_flow += minim;
}
}
cout << max_flow;
}
};
int main() {
MaxFLow graph;
graph.read();
graph.pump();
}