版本比较
小 W 带你速通网络流全家桶
正文
增加 10 行删除 10 行
65 行未改动6667686970707172737423 行未改动989910010110210210310410510637 行未改动144145146147148148149150151152169 行未改动全部展开32232332432532632632732832933051 行未改动38238338438538638638738838939085 行未改动4764774784794804804814824834849 行未改动494495496497498498499500501502415 行未改动全部展开918919920921922922923924925926113 行未改动全部展开1040104110421043104410441045104610471048115 行未改动全部展开1164116511661167116811681169117065 行未改动bool vis[N];int incf[N], pre[N], mf;//incf:到这个点可以允许的最大流量,pre:在增广路上这个点的前驱节点queue<int> q;bool bfs() {//bfs找增广路 while(!q.empty()) q.pop(); while(!q.empty()) q.pop(); memset(vis, false, sizeof vis); q.push(s); vis[s] = true; incf[s] = INF;//源点初始可以流出无限的流量,所以这里是 INF23 行未改动 e[i ^ 1].w += incf[t];//记住,这里一定是增流,不要写成 -= incf[t]!!! u = e[i ^ 1].to; } mf += incf[t];//累加最大流 } }}signed main() { memset(head, -1, sizeof head); ios :: sync_with_stdio(0);37 行未改动struct node { int to, w, nex; } e[M];int head[N], tot = -1;void adde(int u, int v, int w) { e[++ tot] = {v, w, head[u]}; head[u] = tot; head[u] = tot;}int mf, dep[N];queue<int> q;bool bfs(int s) {//bfs 分层169 行未改动全部展开struct node { int to, w, nex; } e[M];int head[N], tot = -1;void adde(int u, int v, int w) { e[++ tot] = {v, w, head[u]}; head[u] = tot; head[u] = tot;}int mf, dep[N];queue<int> q;bool bfs(int s) {51 行未改动 int ans = 0; for(int b = 28; b >= 0; b -= 4) {//按 4 位划分 for(int u = 1; u <= n; u++) { for(int v = u + 1; v <= n; v++) { if((mp[u][v] > 0 || mp[v][u] > 0) && if((mp[u][v] > 0 || mp[v][u] > 0) && (mp[u][v] >= (1 << b) || mp[v][u] >= (1 << b))) { adde(u, v, mp[u][v]);//添加双向边 adde(v, u, mp[v][u]); mp[u][v] = 0;//清除这条边,避免以后加上去第二遍85 行未改动 } } vis[u] = false;//这一句千万不要漏了,不然这一个节点无法重复访问,多路增广也就用不了了 return flow - delta;} }int mflow, mcost;void dinic() { while(spfa(s)) { memcpy(cur, head, sizeof head);9 行未改动 cin >> n >> m >> s >> t; for(int i = 1; i <= m; i++) { int u, v, w, c; cin >> u >> v >> w >> c; adde(u, v, w, c); adde(u, v, w, c); } dinic(); cout << mflow << " " << mcost << endl; return 0;415 行未改动全部展开```cpp#include <bits/stdc++.h>#define fi first#define se second#define endl '\n' #define endl '\n'using namespace std;const int N = 500 + 10;const int M = 1500 + 10;const int INF = 0x3f3f3f3f;113 行未改动全部展开 cin >> Q; while(Q--) { int u, v; cin >> u >> v; u ++; v ++; u ++; v ++; cout << cut[u][v] << endl; } return 0;}115 行未改动全部展开 S = s, T = t, sum = 0;//清零最大流 dinic(); cout << sum << " " << ans << endl; return 0;} }```::::