Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// complexity : m^2 * n
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1004, oo = 1e9+7;
- int n, m, S, T, c[N][N];
- vector<int> adj[N];
- queue<int> Q;
- int tr[N];
- bool vis[N];
- bool BFS() {
- for (int i = 1; i <= n; ++i) vis[i] = false, tr[i] = 0;
- while (Q.size()) Q.pop();
- vis[S] = true; Q.push(S);
- while (Q.size()) {
- int u = Q.front(); Q.pop();
- if (u == T) return true;
- for (int v : adj[u]) {
- if (c[u][v] <= 0 || vis[v]) continue;
- tr[v] = u; vis[v] = true; Q.push(v);
- }
- }
- return false;
- }
- vector<int> Path;
- int res = 0;
- void FixGraph() {
- int v = T; Path.clear();
- while (v != S) { Path.push_back(v); v = tr[v]; } Path.push_back(S);
- reverse(Path.begin(), Path.end());
- int minCost = oo;
- for (int i = 0; i < Path.size()-1; ++i) minCost = min(minCost, c[Path[i]][Path[i+1]]);
- for (int i = 0; i < Path.size()-1; ++i) {
- int u = Path[i], v = Path[i+1];
- c[u][v] -= minCost; c[v][u] += minCost;
- }
- res += minCost;
- }
- void sol() {
- while (true) {
- if (!BFS()) break;
- FixGraph();
- }
- cout << res;
- }
- int main() {
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- cin >> n >> m >> S >> T;
- int u, v, w;
- for (int i = 1; i <= m; ++i) {
- cin >> u >> v >> w;
- adj[u].push_back(v); adj[v].push_back(u);
- c[u][v] = w;
- }
- sol();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement