网络最大流
先放$Dinic$。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define re register 6 #define rep(i, a, b) for (re int i = a; i <= b; ++i) 7 #define repd(i, a, b) for (re int i = a; i >= b; --i) 8 #define maxx(a, b) a = max(a, b); 9 #define minn(a, b) a = min(a, b); 10 #define LL long long 11 #define INF (1 << 30) 12 13 inline int read() { 14 int w = 0, f = 1; char c = getchar(); 15 while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar(); 16 while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar(); 17 return w * f; 18 } 19 20 const int maxn = 1e4 + 5, maxm = 1e5 + 5; 21 22 struct Edge { 23 int u, v, c, f, pre; 24 }; 25 26 struct Graph { 27 queue<int> Q; 28 Edge ed[maxm << 1]; 29 int n, m, G[maxn], s, t; 30 void init(int n) { 31 this->n = n; 32 memset(G, 0, sizeof(G)); 33 m = 1; 34 } 35 void add(int u, int v, int c) { 36 ed[++m] = (Edge){u, v, c, 0, G[u]}; 37 G[u] = m; 38 ed[++m] = (Edge){v, u, 0, 0, G[v]}; 39 G[v] = m; 40 } 41 int vis[maxn], d[maxn]; 42 bool bfs() { 43 memset(vis, 0, sizeof(vis)); 44 vis[s] = 1; 45 Q.push(s); 46 d[s] = 1; 47 while (!Q.empty()) { 48 int u = Q.front(); Q.pop(); 49 for (register int i = G[u]; i; i = ed[i].pre) { 50 Edge &e = ed[i]; 51 if (!vis[e.v] && ed[i].f < ed[i].c) { 52 vis[e.v] = 1; 53 Q.push(e.v); 54 d[e.v] = d[u] + 1; 55 } 56 } 57 } 58 return vis[t]; 59 } 60 int cur[maxn]; 61 int dfs(int u, int a) { 62 if (u == t || a == 0) return a; 63 int flow = 0, f; 64 for (register int &i = cur[u]; i; i = ed[i].pre) { 65 Edge &e = ed[i]; 66 if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.c-e.f)))) { 67 e.f += f; 68 ed[i^1].f -= f; 69 flow += f; 70 a -= f; 71 if (a == 0) break; 72 } 73 } 74 return flow; 75 } 76 int maxflow(int s, int t) { 77 this->s = s, this->t = t; 78 int flow = 0; 79 while (bfs()) { 80 rep(i, 1, n) cur[i] = G[i]; 81 flow += dfs(s, INF); 82 } 83 return flow; 84 } 85 } G; 86 87 int n, m, s, t; 88 89 int main() { 90 n = read(), m = read(), s = read(), t = read(); 91 G.init(n); 92 rep(i, 1, m) { 93 int u = read(), v = read(), c = read(); 94 G.add(u, v, c); 95 } 96 printf("%d", G.maxflow(s, t)); 97 return 0; 98 }
$ISAP$之后再放,学习中 。
最小费用最大流(以例题[洛谷P2045]的源码),用的是$BellmanFord$。可以处理除负环其他情况。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define re register 6 #define rep(i, a, b) for (re int i = a; i <= b; ++i) 7 #define repd(i, a, b) for (re int i = a; i >= b; --i) 8 #define maxx(a, b) a = max(a, b); 9 #define minn(a, b) a = min(a, b); 10 #define LL long long 11 #define INF (1 << 30) 12 13 inline int read() { 14 int w = 0, f = 1; char c = getchar(); 15 while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar(); 16 while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar(); 17 return w * f; 18 } 19 20 const int maxn = 50 * 50 + 5; 21 22 struct Edge { 23 int u, v, c, f, w, pre; 24 }; 25 26 struct Graph { 27 Edge ed[maxn << 3]; 28 int n, m, G[maxn << 1]; 29 void init(int n) { 30 this->n = n; 31 m = 1; 32 memset(G, 0, sizeof(G)); 33 } 34 void add(int u, int v, int c, int w) { 35 ed[++m] = (Edge){u, v, c, 0, w, G[u]}; 36 G[u] = m; 37 ed[++m] = (Edge){v, u, 0, 0, -w, G[v]}; 38 G[v] = m; 39 } 40 int a[maxn << 1], d[maxn << 1], inq[maxn << 1], p[maxn << 1]; 41 int s, t; 42 queue<int> Q; 43 bool bellmanford(int &flow, int &cost) { 44 memset(inq, 0, sizeof(inq)); 45 rep(i, 1, n) d[i] = INF; 46 inq[s] = 1; Q.push(s); p[s] = 0; a[s] = INF; d[s] = 0; 47 while (!Q.empty()) { 48 int u = Q.front(); Q.pop(); inq[u] = 0; 49 for (register int i = G[u]; i; i = ed[i].pre) { 50 Edge &e = ed[i]; 51 if (e.f < e.c && d[u] + e.w < d[e.v]) { 52 d[e.v] = d[u] + e.w; 53 a[e.v] = min(a[u], e.c-e.f); 54 p[e.v] = i; 55 if (!inq[e.v]) { 56 Q.push(e.v); 57 inq[e.v] = 1; 58 } 59 } 60 } 61 } 62 if (d[t] == INF) return false; 63 flow += a[t]; 64 cost += a[t] * d[t]; 65 for (register int i = t; p[i]; i = ed[p[i]].u) { 66 ed[p[i]].f += a[t]; 67 ed[p[i]^1].f -= a[t]; 68 } 69 return true; 70 } 71 int mincostmaxflow(int s, int t, int &cost) { 72 this->s = s, this->t = t; 73 int flow = 0; cost = 0; 74 while (bellmanford(flow, cost)); 75 return flow; 76 } 77 } G; 78 79 int n, k; 80 81 #define id(x, y) ((x)*n+(y)-n) 82 83 int main() { 84 n = read(), k = read(); 85 G.init(n*n*2+2); 86 rep(i, 1, n) 87 rep(j, 1, n) { 88 //a[i][j] = read(); 89 G.add(id(i, j), id(i, j)+n*n, 1, -read()); 90 G.add(id(i, j), id(i, j)+n*n, k, 0); 91 if (i != n) G.add(id(i, j)+n*n, id(i+1, j), k, 0); 92 if (j != n) G.add(id(i, j)+n*n, id(i, j+1), k, 0); 93 } 94 G.add(n*n*2+1, 1, k, 0); 95 G.add(n*n*2, n*n*2+2, k, 0); 96 int cost; 97 G.mincostmaxflow(n*n*2+1, n*n*2+2, cost); 98 printf("%d", -cost); 99 return 0; 100 }