网络流

it2022-07-06  182

#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn = 205; const int inf = 0x3f3f3f3f; int n, m, s, t, maxflow; int h[maxn], e[maxn], ne[maxn], w[maxn], pre[maxn], incf[maxn], idx; bool vis[maxn]; inline void add(int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; e[idx] = a, w[idx] = 0, ne[idx] = h[b], h[b] = idx ++; } inline bool bfs(void) { memset(vis, false, sizeof vis); queue<int> q; q.push(s); vis[s] = true; incf[s] = inf; while(q.size()) { int u = q.front(); q.pop(); for(int i = h[u]; i != -1; i = ne[i]) { if(w[i]) { int v = e[i]; if(vis[v]) continue; incf[v] = min(incf[u], w[i]); pre[v] = i; q.push(v); vis[v] = true; if(v == t) return true; } } } return false; } inline void update(void) { int x = t; while(x != s) { int i = pre[x]; w[i] -= incf[t]; w[i ^ 1] += incf[t]; x = e[i ^ 1]; } maxflow += incf[t]; } int main(void) { // freopen("in.txt", "r", stdin); scanf("%d%d", &m, &n); memset(h, -1, sizeof h); s = 1, t = n; for(int i = 1; i <= m; i ++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c); } while(bfs()) update(); printf("%d\n", maxflow); // fclose(s/tdin); return 0; }

最新回复(0)