版本比较
网络流
正文
增加 23 行删除 1680 行
1223451,092 行未改动全部展开1098109911001101110311041105110211071108110311041110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152110511061155115611571107115911601108110911621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253111011111256125712581112126012611262111311141264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131311151116111711182 行未改动1121112211231124132413251326112511261328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135311271128135613571358112913601361113011311363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414113211331134113511361137113811391423142414251426114014281429114111421431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081143114411451146114715141515151611481518151911491150152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157211511152115311544 行未改动1159116011611162158515861587116315891590159111641165159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481166116716511652165316541168165616571658116911701660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172411711172117311741175173017311732117617341735173617371738117711781740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351179118011811182118311841842184318441845118518471848184911861187185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189311881189119011913 行未改动1195119611971198190519061907119919091910191112001201191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197012021203120412051975197619771978120619801981198212071208198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028120912101211121220332034203520361213203820392040121412152042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096121612171218121912201221122212232105210621071224210921101225122621122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223112271228122922352236223712302239224012311232224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232012331234232323242325123523272328123612372330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405123812391240240924102411124112422413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724781243124412452482248324841246248624871247124824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625671249125012511252125312541255257525762577125625792580258112571258258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666125912601261126212631264126526742675267626771266267926802681268226832684268526862687268826892690269112671268269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277412691270127112721273127427812782278312752785278612761277278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928701278127912802874287528761281287828792880128212832882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294012841285128612871288ps.:由于篇幅限制,本篇文章很多地方没有放图,太占空间,文章涉及到“自行推导”的字眼一般都是很显然的,读者如果不能一眼还是建议手推一下。 (其实是因为作者太懒了,如果有例题的图,欢迎留言)
# 一、基本模型与概念## 1.网络流模型网络流模型是指一个边**带权值**且**连通**的**有向图**,并且该图中只存在**一个入度为 0** 的点(源点 $S$)和 **一个出度为 0** 的点(汇点 $T$)。1,092 行未改动全部展开
## [1.飞行员配对方案问题](https://www.luogu.com.cn/problem/P2756)其实就是给定一个二分图,求最大匹配并构造方案。 可以直接匈牙利 + Konig 解决,可以参考[二分图](https://www.luogu.com.cn/blog/511676/er-fen-tu)。当然用网络流也是可以的。```#include<bits/stdc++.h>using namespace std;
const int N = 110, M = 5210;int n, m, ans;<https://www.luogu.com.cn/record/137138840>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n + m + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n + m + 2) return outflow; } if(!--cnt[dis[x]]) return outflow;//洛谷 SPJ 炸了,模板过不去,这样能过…… cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> m >> n; int a, b; while(cin >> a >> b, ~a && ~b) add(a, b, 1), add(b, a, 0); for(int i = 1; i <= m; i++) add(0, i, 1), add(i, 0, 0); for(int i = m + 1; i <= n; i++) add(i, n + m + 1, 1), add(n + m + 1, i, 0); for(int i = 0; i <= n + m + 1; i++) cur[i] = h[i]; while(dis[0] < n + m + 2) ans += sap(0, 1e9); cout << ans << endl; for(int i = 1; i <= m; i++) for(int j = h[i]; j; j = ne[j]) if(e[j] && !w[j]) cout << i << ' ' << e[j] << endl; return 0;}```
## [2.太空飞行计划问题](https://www.luogu.com.cn/problem/P2762) 见上。```#include<bits/stdc++.h>using namespace std;
const int N = 110, M = 5110;int n, m, ans; <https://www.luogu.com.cn/record/137667536>
char buf[1 << 21], *p1 = buf, *p2 = buf;inline char gc(){ if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin); return p1 == p2 ? EOF : *p1++;}inline int read(){ int f = 1, w = 0; char ch = gc(); while((ch < '0' || '9' < ch) && ch != '\n') ch = gc(); if(ch == '\n') return -1; while('0' <= ch && ch <= '9') { w = (w << 1) + (w << 3) + (ch ^ 48); ch = gc(); } p1--; return f * w;}
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n + m + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n + m + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n + m + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
bool flag[N];int q[N], tt;void bfs(){ flag[q[++tt] = 0] = 1; while(tt) { int t = q[tt--]; for(int i = h[t]; i; i = ne[i]) if(w[i] && !flag[e[i]]) flag[q[++tt] = e[i]] = 1; }}
int main(){ n = read(), m = read(); for(int i = 1, x; i <= n; i++) { while(x = read()) if(~x) break; ans += x; add(0, i, x), add(i, 0, 0); while(x = read(), ~x) add(i, n + x, 1e9), add(n + x, i, 0); } for(int i = 1, x; i <= m; i++) { while(x = read()) if(~x) break; add(n + i, n + m + 1, x), add(n + m + 1, n + i, 0); } for(int i = 0; i <= n + m + 1; i++) cur[i] = h[i]; while(dis[0] < n + m + 2) ans -= sap(0, 1e9); bfs(); for(int i = 1; i <= n; i++) if(flag[i]) cout << i << ' '; puts(""); for(int i = 1; i <= m; i++) if(flag[n + i]) cout << i << ' '; cout << endl << ans << endl; return 0;}```
## [3.最小路径覆盖问题](https://www.luogu.com.cn/problem/P2764)在[二分图](https://www.luogu.com.cn/blog/511676/er-fen-tu)里面有谈到对于 DAG 的最小路径覆盖问题,无非就是把每个点拆成入度和出度两个点处理。当然这个题也可用匈牙利。```#include<bits/stdc++.h>using namespace std;
const int N = 310, M = 12610;int n, m, ans, to[N];bool flag[N];<https://www.luogu.com.cn/record/136982069>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == 2 * n + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(outflow == inflow || dis[0] >= 2 * n + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = 2 * n + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> n >> m; for(int i = 1, a, b; i <= m; i++) scanf("%d%d", &a, &b), add(a, n + b, 1), add(n + b, a, 0); for(int i = 1; i <= n; i++) add(0, i, 1), add(i, 0, 0), add(n + i, 2 * n + 1, 1), add(2 * n + 1, n + i, 0); for(int i = 0; i <= 2 * n + 1; i++) cur[i] = h[i]; while(dis[0] < 2 * n + 2) ans += sap(0, 1e9); for(int i = 1; i <= n; i++) for(int j = h[i]; j; j = ne[j]) if(e[j] && !w[j]) flag[to[i] = e[j] - n] = 1; for(int i = 1; i <= n; i++) if(!flag[i]) { for(int j = i; j; j = to[j]) printf("%d ", j); puts(""); } cout << n - ans << endl; return 0;}```
## [4.魔术球问题](https://www.luogu.com.cn/problem/P2765)谁说网络流 $24$ 题必须写网络流的!!! 众所周知,这道题出现在了 CSP-S 初赛 T5 的神奇位置……不过当时数据更小,可以手推。当时我使用的就是贪心的做法,这里仍旧贪心,算是纪念。
2 行未改动
另外,其实第一问的答案有结论:$+2+4+4+6+6+8+8\dots$ 通项:$\frac{n\times(n+2)+(n\bmod2)-2}2$
```#include<bits/stdc++.h>using namespace std;<https://www.luogu.com.cn/record/138575712>
const int N = 60;int n, ans;vector<int> res[N];
int main(){ cin >> n; for(int i = 1; i <= 10000; i++) { bool flag = 0; for(int j = 1; !flag && j <= n; j++) if(res[j].empty() || sqrt(res[j].back() + i) == (int)sqrt(res[j].back() + i)) ans++, res[j].push_back(i), flag = 1; if(!flag) break; } cout << ans << endl; for(int i = 1; i <= n; i++) { for(int j : res[i]) cout << j << ' '; puts(""); } return 0;}```
## [5.圆桌问题](https://www.luogu.com.cn/problem/P3254)简单题,把人当做水,建虚拟点 $s,t$,每个单位的代表人数 $r_i$ 意味着 $c(s,i)=r_i$,每个圆桌的人数限制 $c_j$ 意味着 $c(j,t)=c_j$,相同单位的每个人的圆桌都不相同意味着 $c(i,j)=1$。跑最大流,如果 $maxf=\sum r_i$ 则有解。```#include<bits/stdc++.h>using namespace std;
const int N = 430, M = 81850;int n, m, ans;<https://www.luogu.com.cn/record/136813729>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n + m + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n + m + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n + m + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> n >> m; for(int i = 1, a; i <= n; i++) cin >> a, add(0, i, a), add(i, 0, 0), ans += a; for(int i = 1, a; i <= m; i++) cin >> a, add(n + i, n + m + 1, a), add(n + m + 1, n + i, 0); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) add(i, n + j, 1), add(n + j, i, 0);
for(int i = 0; i <= n + m + 1; i++) cur[i] = h[i]; while(dis[0] < n + m + 2) ans -= sap(0, 1e9); if(ans) puts("0"); else { puts("1"); for(int i = 1; i <= n; i++) { for(int j = h[i]; j; j = ne[j]) if(e[j] && !w[j]) printf("%d ", e[j] - n); puts(""); } } return 0;}```
## [6.最长不下降子序列问题](https://www.luogu.com.cn/problem/P2766)第一问显然。(关于我第一问求错了,后面两问对了这件事……)
第二问先将题目给定的偏序集建出来(建立虚拟源点和虚拟汇点)。于是题目转化为了最多可以划分成多少个长度为 $s$ 的路径。注意到如果没有 $s$ 的限制就好做多了,于是这玩意可以先跑一边最长路,检查一下每条边是否在最长路上,把那些不在的边去掉即可。 接着以源点和汇点直接跑最大流显然就是答案。
第三问就是第二问的 plus 版。只要懂了第二问,那么第三问就很简单了:把源点和 $1$ 号点之间、$n$ 号点和汇点之间分别连一条容量无穷大的边即可。 事实上,这玩意不需要重新开始跑。由于在残留网络上添加新边之后,可以接着跑最大流,所以无需清空,在第二问的基础上继续加即可。```#include<bits/stdc++.h>using namespace std;#define PII pair<int, int>
const int N = 510, M = 1e6 + 10;int n, a[N], f[N], s, ans;<https://www.luogu.com.cn/record/136661434>
int h[N], h1[N], h2[N], ne[M], e[M], w[M], idx = 1;void add(int h[], int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis1[N], dis2[N];void dijkstra(int dis[], int h[], int s){ priority_queue<PII, vector<PII>, greater<PII> > q; q.push({0, s}); while(!q.empty()) { PII t = q.top(); q.pop(); if(t.first != dis[t.second]) continue; for(int i = h[t.second]; i; i = ne[i]) { int j = e[i]; if(dis[j] < t.first + w[i]) q.push({dis[j] = t.first + w[i], j}); } }}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n + 2; cnt[++dis[x]]++, cur[x] = h2[x]; return outflow;}
int main(){ cin >> n; for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) { f[i] = 1; for(int j = 1; j < i; j++) if(a[j] <= a[i]) f[i] = max(f[i], f[j] + 1); s = max(s, f[i]); } cout << s << endl; for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) if(a[i] <= a[j]) add(h, i, j, 1), add(h1, j, i, 1); for(int i = 1; i <= n; i++) add(h, 0, i, 1), add(h, i, n + 1, 1), add(h1, n + 1, i, 1), add(h1, i, 0, 1); dijkstra(dis1, h, 0), dijkstra(dis2, h1, n + 1); for(int i = 0; i <= n + 1; i++) for(int j = h[i]; j; j = ne[j]) if(dis1[i] + 1 + dis2[e[j]] == dis1[n + 1]) add(h2, i, e[j], 1), add(h2, e[j], i, 0); for(int i = 0; i <= n + 1; i++) cur[i] = h2[i]; while(dis[0] < n + 2) ans += sap(0, 1e9); cout << ans << endl; add(h2, 0, 1, 1e9), add(h2, 1, 0, 0), add(h2, n, n + 1, 1e9), add(h2, n + 1, n, 0); memset(dis, 0, sizeof(dis)); memset(cnt, 0, sizeof(cnt)); for(int i = 0; i <= n + 1; i++) cur[i] = h2[i]; while(dis[0] < n + 2) ans += sap(0, 1e9); if(s == 1) ans = n;//特判 cout << ans << endl; return 0;}```
## [7.试题库问题](https://www.luogu.com.cn/problem/P2763)简单建模。 题面有些模糊,大意是每个题都有对应的类型,你可以决定将他归入哪一类(也就是最后每道题只有一种类型),最后使得每一类的题目数等于给定的值。
将类型看做左部的点、题目看做右部的点建立二分图,每道题向他对应的类型连边,边权为 $1$。然后把题目当做水,建立源点汇点,看它是否可以分配成功即可。(最大流是否等于 $m$)```#include<bits/stdc++.h>using namespace std;
const int N = 1020, M = 42050;int n, m, ans;<https://www.luogu.com.cn/record/136968221>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n + m + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(outflow == inflow || dis[0] >= n + m + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n + m + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> n >> m; for(int i = 1, a; i <= n; i++) scanf("%d", &a), ans += a, add(0, i, a), add(i, 0, 0); for(int i = 1, k, x; i <= m; i++) { scanf("%d", &k); while(k--) scanf("%d", &x), add(x, n + i, 1), add(n + i, x, 0); add(n + i, n + m + 1, 1), add(n + m + 1, n + i, 0); }
for(int i = 0; i <= n + m + 1; i++) cur[i] = h[i]; while(dis[0] < n + m + 2) ans -= sap(0, 1e9); if(ans) puts("No Solution!"); else for(int i = 1; i <= n; i++) { printf("%d: ", i); for(int j = h[i]; j; j = ne[j]) if(e[j] && !w[j]) printf("%d ", e[j] - n); puts(""); } return 0;}```
## [8.机器人路径规划问题](https://www.luogu.com.cn/problem/P2775)**网络流 24 题中唯一一道暂未解决的题目!**
对于这种题,我想如果把它作为论文题看待应该更合适。另外,由于数据本身有误,我们不以通过此题作为研究目标,而应是在尽可能优秀的复杂度内解决该问题。4 行未改动首先将问题转化为:从方格中删去一些数,使得删掉的数字总和最小。
这道题的突破口在于**二分图**,因为将相邻元素相连形成的图显然是二分图。由于删掉元素本身不好处理,所以这张初始的图很难再去表示“代价”这个信息了。考虑用额外的边表示代价,即从每个点引一条边出去,边权是点权。画出这张图,思考这张图怎么表示答案。 由于图上相邻元素不能同时选,容易想到用**最小割**的方式表示。显然割的是点不是边,也就是说那些代表点权的边是可以割的,连接关系的边不应该割掉,权值赋值为 $+\infty$ 即可。建立源点 $s$ 表示左部的点,$t$ 表示右部的点,如果这个点没有被选,则代表这条边是割边。容易证明对于所有合法方案,这样割掉之后 $s,t$ 都不再连通。对这张图跑最小割,就是被删掉的数之和。```#include<bits/stdc++.h>using namespace std;
const int N = 10010, M = 60010;int n, m, ans;int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};<https://www.luogu.com.cn/record/136823472>
inline int get(int x, int y){ return (x - 1) * m + y;}
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n * m + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n * m + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n * m + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> n >> m; for(int i = 1, x; i <= n; i++) for(int j = 1; j <= m; j++) { scanf("%d", &x), ans += x; if(i + j & 1) add(0, get(i, j), x), add(get(i, j), 0, 0); else add(get(i, j), n * m + 1, x), add(n * m + 1, get(i, j), 0); } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(i + j & 1) for(int k = 0; k < 4; k++) { int x = i + dx[k], y = j + dy[k]; if(1 <= x && x <= n && 1 <= y && y <= m) add(get(i, j), get(x, y), 1e9), add(get(x, y), get(i, j), 0); } for(int i = 0; i <= n * m + 1; i++) cur[i] = h[i]; while(dis[0] < n * m + 2) ans -= sap(0, 1e9); cout << ans << endl; return 0;}```
## [10.餐巾计划问题](https://www.luogu.com.cn/problem/P1251)有点类似上面的 [P2469 [SDOI2010]星际竞速](https://www.luogu.com.cn/problem/P2469)。 ```#include<bits/stdc++.h>using namespace std;#define LL long long
const int N = 4010, M = 20010;int n, r[N], p, cost1, cost2, day1, day2;LL ans;<https://www.luogu.com.cn/record/137799479>
int h[N], ne[M], e[M], w1[M], idx = 1;LL w[M];void add(int a, int b, LL c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}
LL flow[N], dis[N];int pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, 0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e18, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] > dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[2 * n + 1];}
int main(){ cin >> n; for(int i = 1; i <= n; i++) scanf("%d", &r[i]), add(0, n + i, r[i], 0), add(n + i, 0, 0, 0); cin >> p >> day1 >> cost1 >> day2 >> cost2; add(0, 1, 1e18, p), add(1, 0, 0, -p); for(int i = 1; i <= n; i++) { if(i < n) add(i, i + 1, 1e18, 0), add(i + 1, i, 0, 0); if(i + day1 <= n) add(n + i, i + day1, 1e18, cost1), add(i + day1, n + i, 0, -cost1); if(i + day2 <= n) add(n + i, i + day2, 1e18, cost2), add(i + day2, n + i, 0, -cost2); add(i, 2 * n + 1, r[i], 0), add(2 * n + 1, i, 0, 0); }
while(ssp()) { ans += flow[2 * n + 1] * dis[2 * n + 1]; for(int i = 2 * n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * n + 1], w[pre[i] ^ 1] += flow[2 * n + 1]; } cout << ans << endl; return 0;}```
## [11.航空路线问题](https://www.luogu.com.cn/problem/P2770)首先考虑求城市数。显然最后的形态是一个环,我们将它变为两条链,那么每条边都一定是从左往右连的。其中 $2\sim n-1$ 这些点显然只能经过一次,这可以拆点解决。于是我们连边 $s\xrightarrow21,n\xrightarrow2t$,跑最大流即可表示一种方案。为了使城市最多,给每个城市加上一个“费用 $1$”,跑费用流即可。 方案输出也不难,无非就是找到那两条链,将其中一条倒序输出即可。
注意特殊情况:$1\to n\to1$,特判即可。```#include<bits/stdc++.h>using namespace std;
const int N = 210, M = 20210;int n, m, ans1, ans2;string s[N];unordered_map<string, int> id;vector<int> s1, s2;<https://www.luogu.com.cn/record/138668286>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e9, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[2 * n + 1];}
void dfs(int x, vector<int> &s){ s.push_back(x); if(x == n) return; for(int i = h[n + x]; i; i = ne[i]) if(!w[i]) dfs(e[i], s);}
int main(){ cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> s[i]; id[s[i]] = i; } for(int i = 1, a, b; i <= m; i++) { string s1, s2; cin >> s1 >> s2; a = id[s1], b = id[s2]; add1(n + min(a, b), max(a, b), 1, 0); add1(n + min(a, b), max(a, b), 1, 0); } for(int i = 2; i < n; i++) add1(i, n + i, 1, 1); add1(0, n + 1, 2, 0), add1(n, 2 * n + 1, 2, 0);
while(ssp()) { ans1 += flow[2 * n + 1], ans2 += flow[2 * n + 1] * dis[2 * n + 1]; for(int i = 2 * n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * n + 1], w[pre[i] ^ 1] += flow[2 * n + 1]; } if(ans1 != 2) return puts("No Solution!"), 0; cout << ans2 + 2 << endl; for(int i = h[n + 1]; i; i = ne[i]) if(e[i] && !w[i]) { if(s1.empty()) dfs(e[i], s1); else dfs(e[i], s2); } cout << s[1] << endl; for(int i : s1) cout << s[i] << endl; s2.pop_back(); reverse(s2.begin(), s2.end()); for(int i : s2) cout << s[i] << endl; cout << s[1] << endl; return 0;}```
## [12.软件补丁问题](https://www.luogu.com.cn/problem/P2761)~~没看出来和网络流的关系?据说可以费用流,有懂的大佬欢迎补充下。~~
首先数据范围一眼状压。 容易发现只有 $2^{20}$ 种状态,考虑状压 DP,但是会有后效性。转移式非常简单,观察其本质,事实上就是一个最短路而已。直接上一个 dijkstra 即可。 代码实现中没有建图,因为可以直接转移。```#include<bits/stdc++.h>using namespace std;#define PII pair<int, int>
const int N = 1 << 22, M = 110;int n, m, dis[N], c[N], a1[N], a2[N], b1[N], b2[N];char s[30];<https://www.luogu.com.cn/record/137267683>
void init(int &a, int &b){ scanf("%s", s); for(int i = 0; i < n; i++) if(s[i] == '+') a |= 1 << i; else if(s[i] == '-') b |= 1 << i;}
void bfs(){ memset(dis, 0x3f, sizeof(dis)); priority_queue<PII, vector<PII>, greater<PII> > q; q.push({dis[(1 << n) - 1] = 0, (1 << n) - 1}); while(!q.empty()) { PII t = q.top(); q.pop(); if(t.first != dis[t.second]) continue; for(int i = 1; i <= m; i++) if((t.second & a1[i]) == a1[i] && (t.second & a2[i]) == 0) { int j = t.second & ~b1[i] | b2[i]; if(dis[j] > t.first + c[i]) q.push({dis[j] = t.first + c[i], j}); } }}
int main(){ cin >> n >> m; for(int i = 1; i <= m; i++) { cin >> c[i]; init(a1[i], a2[i]), init(b2[i], b1[i]); } bfs(); cout << (dis[0] <= 1e9 ? dis[0] : 0) << endl; return 0;}```
## [13.星际转移问题](https://www.luogu.com.cn/problem/P2754)分层图模板题?
显然答案不会太大,考虑暴枚答案。由于不同时间每个飞船的路线不一样,不太方便处理。我们可以把每个点都拆成 $t$ 个($t$ 表示时间),然后每个飞船连边的时候显然除了位置改变,时间是要随之 $+1$ 的。画在图上就变成了分层图的形式,前一层向下一层连边。3 行未改动- 跑二分,虽然带 $\log$,但常数肯定比上面小;- 用 EK $\operatorname{or}$ Dinic,事实上,在分层图里,EK 的复杂度会减少到和 ISAP 同级,且常数显然很小,可以通过;当然 Dinic 更优,因为 Dinic 本身就是支持跑残留网络的,用不着我们 ISAP 的清空操作。
最后一句话:判无解!!!(调死我了)```#include<bits/stdc++.h>using namespace std;
const int N = 5e5 + 10, M = 1e6 + 10;int n, m, k, ans, r[N];vector<int> s[N];<https://www.luogu.com.cn/record/137598893>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
int flow[N], pre[N];int q[N], hh, tt;bool EK(){ memset(flow, 0, sizeof(flow)); flow[q[hh = tt = 1] = 0] = k; while(hh <= tt) { int t = q[hh++]; if(t == n + 1) return true; for(int i = h[t]; i; i = ne[i]) if(w[i] && !flow[e[i]]) flow[q[++tt] = e[i]] = min(flow[t], w[i]), pre[e[i]] = i; } return false;}
int main(){ cin >> n >> m >> k; for(int i = 1, cnt, x; i <= m; i++) { cin >> r[i] >> cnt; while(cnt--) cin >> x, s[i].push_back(x == -1 ? n + 1 : x); } for(int i = 1; i <= 1000; i++) { for(int j = i * (n + 2); j < (i + 1) * (n + 2); j++) add(j - n - 2, j, 1e9), add(j, j - n - 2, 0); add(i * (n + 2) + n + 1, n + 1, 1e9), add(n + 1, i * (n + 2) + n + 1, 0); for(int j = 1; j <= m; j++) if(i % s[j].size() == 0) add((i - 1) * (n + 2) + s[j].back(), i * (n + 2) + s[j][0], r[j]), add(i * (n + 2) + s[j][0], (i - 1) * (n + 2) + s[j].back(), 0); else add((i - 1) * (n + 2) + s[j][i % s[j].size() - 1], i * (n + 2) + s[j][i % s[j].size()], r[j]), add(i * (n + 2) + s[j][i % s[j].size()], (i - 1) * (n + 2) + s[j][i % s[j].size() - 1], 0); while(EK()) { ans += flow[n + 1]; for(int i = n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[n + 1], w[pre[i] ^ 1] += flow[n + 1]; } if(ans >= k) { cout << i << endl; return 0; } } puts("0"); return 0;}```
## [14.孤岛营救问题](https://www.luogu.com.cn/problem/P4011)也不需要网络流,bfs 即可解决。
鉴于 P 很小,考虑状压。于是直接 bfs 就可以过了。另外在实现的时候,没有必要区分墙和门,因为显然找不到 $0$ 这种钥匙,直接把墙当做需要 $0$ 号钥匙的门即可。```#include<bits/stdc++.h>using namespace std;#define PIII pair<pair<int, int>, int>
const int N = 15, M = 1 << N;int n, m, p, k, f[N][N][M], a[N][N][4], b[N][N];int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};<https://www.luogu.com.cn/record/138679559>
void bfs(){ memset(f, 0x3f, sizeof(f)); queue<PIII> q; f[1][1][b[1][1]] = 0, q.push({{1, 1}, b[1][1]}); while(!q.empty()) { PIII t = q.front(); q.pop(); int x = t.first.first, y = t.first.second, s = t.second; if(x == n && y == m) { cout << f[x][y][s] << endl; exit(0); } for(int i = 0; i < 4; i++) { int x1 = x + dx[i], y1 = y + dy[i]; if(1 <= x1 && x1 <= n && 1 <= y1 && y1 <= m && a[x][y][i] != -1 && (a[x][y][i] | s) == s && f[x1][y1][s | b[x1][y1]] > f[x][y][s] + 1) f[x1][y1][s | b[x1][y1]] = f[x][y][s] + 1, q.push({{x1, y1}, s | b[x1][y1]}); } }}
int main(){ cin >> n >> m >> p >> k; for(int i = 1, x1, y1, x2, y2, g; i <= k; i++) { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &g); if(x1 == x2) a[x1][min(y1, y2)][1] = a[x1][max(y1, y2)][3] = 1 << g; else a[min(x1, x2)][y1][2] = a[max(x1, x2)][y1][0] = 1 << g; } cin >> k; for(int i = 1, x, y, z; i <= k; i++) { scanf("%d%d%d", &x, &y, &z); b[x][y] |= 1 << z; }
bfs(); puts("-1"); return 0;}```
## [15.汽车加油行驶问题](https://www.luogu.com.cn/problem/P4009)鉴于 $k$ 的范围过小,考虑分层图,用层数表示油数,暴力建图即可。这样的边数、点数在普通的 ssp 算法中会 TLE,但是考虑到这里的总流量只有 $1$,所以相当于只是跑了一遍最短路而已。
注:当经过加油站的时候,**强制**加油,不能不加!```#include<bits/stdc++.h>using namespace std;#define PII pair<int, int>
const int N = 110010, M = 500100, K = 110;int n, m, a, b, c, x[K][K];int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};<https://www.luogu.com.cn/record/140191315>
int h[N], ne[M], e[M], w[M], idx;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}
inline int get(int x, int y, int z){ return (x - 1) * n + y + z * n * n;}
int dis[N];void dijkstra(){ memset(dis, 0x3f, sizeof(dis)); priority_queue<PII, vector<PII>, greater<PII> > q; q.push({dis[get(1, 1, m)] = 0, get(1, 1, m)}); while(!q.empty()) { PII t = q.top(); q.pop(); if(t.first != dis[t.second]) continue; for(int i = h[t.second]; i; i = ne[i]) { int j = e[i]; if(dis[j] > t.first + w[i]) q.push({dis[j] = t.first + w[i], j}); } }}
int main(){ cin >> n >> m >> a >> b >> c; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { scanf("%d", &x[i][j]); for(int k = 0; k < m; k++) add(get(i, j, k), get(i, j, m), x[i][j] ? a : a + c); for(int k = x[i][j] ? m : 1; k <= m; k++) for(int t = 0; t < 4; t++) { int x = i + dx[t], y = j + dy[t]; if(1 <= x && x <= n && 1 <= y && y <= n) add(get(i, j, k), get(x, y, k - 1), t <= 1 ? b : 0); } } for(int i = 1; i <= m; i++) add(get(n, n, i), get(n, n, i - 1), 0); dijkstra(); cout << dis[get(n, n, 0)] << endl; return 0;}```
## [16.数字梯形问题](https://www.luogu.com.cn/problem/P4013)又是缝合怪?都是最大费用最大流,建图不同,每一问拆开做。
1. 对于第一问,实际上就是限制了每一个点的流量至多为 $1$,拆点即可。2. 第二问实际上就是把点的流量不超过 $1$ 改为了边的流量不超过 $1$,反而更简单了。3. 第三问更简单,去掉了所有的流量限制。
注意在复制代码的时候不要粗心大意,多测清空即可。```#include<bits/stdc++.h>using namespace std;
const int N = 1610, M = 4930;int n, m, a[30][50], s, t = 1601;<https://www.luogu.com.cn/record/140213256>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
inline int get(int x, int y){ return (x - 1) * 40 + y;}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[s] = 1e9, dis[s] = 0, in[q[tt++] = s] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[t];}
void work1(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= m + i - 1; j++) { add1(get(i, j), 800 + get(i, j), 1, a[i][j]); if(i < n) add1(800 + get(i, j), get(i + 1, j), 1, 0), add1(800 + get(i, j), get(i + 1, j + 1), 1, 0); } for(int i = 1; i <= m; i++) add1(s, get(1, i), 1, 0); for(int i = 1; i <= n + m - 1; i++) add1(800 + get(n, i), t, 1, 0); int ans = 0; while(ssp()) { ans += flow[t] * dis[t]; for(int i = t; i != s; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t]; } cout << ans << endl;}
void work2(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= m + i - 1; j++) if(i < n) add1(get(i, j), get(i + 1, j), 1, a[i + 1][j]), add1(get(i, j), get(i + 1, j + 1), 1, a[i + 1][j + 1]); for(int i = 1; i <= m; i++) add1(s, get(1, i), 1, a[1][i]); for(int i = 1; i <= n + m - 1; i++) add1(get(n, i), t, m, 0); int ans = 0; while(ssp()) { ans += flow[t] * dis[t]; for(int i = t; i != s; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t]; } cout << ans << endl;}
void work3(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= m + i - 1; j++) if(i < n) add1(get(i, j), get(i + 1, j), m, a[i + 1][j]), add1(get(i, j), get(i + 1, j + 1), m, a[i + 1][j + 1]); for(int i = 1; i <= m; i++) add1(s, get(1, i), 1, a[1][i]); for(int i = 1; i <= n + m - 1; i++) add1(get(n, i), t, m, 0); int ans = 0; while(ssp()) { ans += flow[t] * dis[t]; for(int i = t; i != s; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t]; } cout << ans << endl;}
int main(){ cin >> m >> n; for(int i = 1; i <= n; i++) for(int j = 1; j <= m + i - 1; j++) scanf("%d", &a[i][j]); work1(); memset(h, 0, sizeof(h)), idx = 1; work2(); memset(h, 0, sizeof(h)), idx = 1; work3(); return 0;}```
## [17.运输问题](https://www.luogu.com.cn/problem/P4015)费用流模板题? 显然是一个二分图,点数和边数规模都不算大,直接根据题意模拟建图即可。注意,由于要求两个答案,不能直接在残留网络上面跑,只能重新建一遍图。```#include<bits/stdc++.h>using namespace std;
const int N = 210, M = 20410;int n, m, ans, a[N], b[N], c[N][N];<https://www.luogu.com.cn/record/140215085>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(bool op){ memset(flow, 0, sizeof(flow)); memset(dis, op ? -0x3f : 0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e9, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(op ? (dis[j] < dis[t] + w1[i]) : (dis[j] > dis[t] + w1[i])) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[n + m + 1];}
int main(){ cin >> n >> m; for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add1(0, i, a[i], 0); for(int i = 1; i <= m; i++) scanf("%d", &b[i]), add1(n + i, n + m + 1, b[i], 0); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &c[i][j]), add1(i, n + j, 1e9, c[i][j]); while(ssp(0)) { ans += flow[n + m + 1] * dis[n + m + 1]; for(int i = n + m + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[n + m + 1], w[pre[i] ^ 1] += flow[n + m + 1]; } cout << ans << endl; memset(h, 0, sizeof(h)), idx = 1; for(int i = 1; i <= n; i++) add1(0, i, a[i], 0); for(int i = 1; i <= m; i++) add1(n + i, n + m + 1, b[i], 0); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) add1(i, n + j, 1e9, c[i][j]); ans = 0; while(ssp(1)) { ans += flow[n + m + 1] * dis[n + m + 1]; for(int i = n + m + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[n + m + 1], w[pre[i] ^ 1] += flow[n + m + 1]; } cout << ans << endl; return 0;}```
## [18.分配问题](https://www.luogu.com.cn/problem/P4014)和前一道完全一样?(甚至更简单?)```#include<bits/stdc++.h>using namespace std;
const int N = 210, M = 20410;int n, ans, a[N][N];<https://www.luogu.com.cn/record/140215960>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(bool op){ memset(flow, 0, sizeof(flow)); memset(dis, op ? -0x3f : 0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e9, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(op ? (dis[j] < dis[t] + w1[i]) : (dis[j] > dis[t] + w1[i])) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[2 * n + 1];}
int main(){ cin >> n; for(int i = 1; i <= n; i++) add1(0, i, 1, 0); for(int i = 1; i <= n; i++) add1(n + i, 2 * n + 1, 1, 0); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &a[i][j]), add1(i, n + j, 1, a[i][j]); while(ssp(0)) { ans += flow[2 * n + 1] * dis[2 * n + 1]; for(int i = 2 * n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * n + 1], w[pre[i] ^ 1] += flow[2 * n + 1]; } cout << ans << endl; memset(h, 0, sizeof(h)), idx = 1; for(int i = 1; i <= n; i++) add1(0, i, 1, 0); for(int i = 1; i <= n; i++) add1(n + i, 2 * n + 1, 1, 0); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) add1(i, n + j, 1, a[i][j]); ans = 0; while(ssp(1)) { ans += flow[2 * n + 1] * dis[2 * n + 1]; for(int i = 2 * n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * n + 1], w[pre[i] ^ 1] += flow[2 * n + 1]; } cout << ans << endl; return 0;}```
## [19.负载平衡问题](https://www.luogu.com.cn/problem/P4016)这里和上面邮票的那道题有点相似,利用的网络流的“自适应性”进行处理。每个点和 $s,t$ 连边,从直观上看是错误的,但是仔细一想由于流量的不同,导致有些水并不能直接流入汇点。这时,这些水便会流经我们的“环”,产生代价。
```#include<bits/stdc++.h>using namespace std;<https://www.luogu.com.cn/record/140216832>
const int N = 110, M = 810;int n, ans;
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d); }
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, 0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e9, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] > dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[n + 1];}
int main(){ cin >> n; int sum = 0; for(int i = 1, x; i <= n; i++) scanf("%d", &x), add1(0, i, x, 0), sum += x; sum /= n; for(int i = 1; i <= n; i++) add1(i, n + 1, sum, 0), add1(i, i == n ? 1 : i + 1, 1e9, 1), add1(i, i == 1 ? n : i - 1, 1e9, 1);
while(ssp()) { ans += flow[n + 1] * dis[n + 1]; for(int i = n + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[n + 1], w[pre[i] ^ 1] += flow[n + 1]; } cout << ans << endl; return 0;}```
## [20.深海机器人问题](https://www.luogu.com.cn/problem/P4012)一眼看上去有点难度,仔细一看,好像并没有规定机器人的具体起点和终点,让我自己分配?这不成水题了吗? ~~然后调坐标调了很久……~~```#include<bits/stdc++.h>using namespace std;
const int N = 310, M = 2080;int cnt1, cnt2, n, m, ans;<https://www.luogu.com.cn/record/140229723>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
inline int get(int x, int y){ return (x - 1) * m + y; }
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = 1e9, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[n * m + 1];}
int main(){ cin >> cnt1 >> cnt2 >> n >> m; n++, m++; for(int i = 1, x; i <= n; i++) for(int j = 1; j < m; j++) scanf("%d", &x), add1(get(i, j), get(i, j + 1), 1, x), add1(get(i, j), get(i, j + 1), 1e9, 0); for(int j = 1, x; j <= m; j++) for(int i = 1; i < n; i++) scanf("%d", &x), add1(get(i, j), get(i + 1, j), 1, x), add1(get(i, j), get(i + 1, j), 1e9, 0); for(int i = 1, k, x, y; i <= cnt1; i++) { scanf("%d%d%d", &k, &x, &y); add1(0, get(x + 1, y + 1), k, 0); } for(int i = 1, k, x, y; i <= cnt2; i++) { scanf("%d%d%d", &k, &x, &y); add1(get(x + 1, y + 1), n * m + 1, k, 0); }
while(ssp()) { ans += flow[n * m + 1] * dis[n * m + 1]; for(int i = n * m + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[n * m + 1], w[pre[i] ^ 1] += flow[n * m + 1]; } cout << ans << endl; return 0;}```
## [21.最长k可重区间集问题](https://www.luogu.com.cn/problem/P3358)费用流建模题,具体的建模参考题解。 但是本人非常的菜,什么都不会,于是使用了一种超级暴力的方法。具体如下:
首先肯定要连一条链,容量为 $k$,费用为 $0$,来限定最多 $k$ 次。但是如何表示区间的代价呢?最开始想的是连接 $s\xrightarrow[0]1l,r\xrightarrow[r-l]{1} t$,但是错误性显然。 考虑如何修改:我们要使得每一组这样的边要么都选,要么都不选才是合法的。于是我建了一个虚点 $i$ 表示这个区间,连边 $s\xrightarrow[0]1i,i\xrightarrow[0]1t$ 表示不选这个区间,连边 $i\xrightarrow[0]1l,r\xrightarrow[r-l]1i$ 表示选择。显然这样跑出来是正确的,但前提是得跑出来…… 具体的,实际上这样是有正环的,跑带正环的最大费用最大流即可。```#include<bits/stdc++.h>using namespace std;
const int N = 1510, M = 8010;int n, m, l[N], r[N], s, t, ans;vector<int> ids;<https://www.luogu.com.cn/record/138414772>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
inline int get(int x){ return lower_bound(ids.begin(), ids.end(), x) - ids.begin() + 1;}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[s] = 1e9, dis[s] = 0, in[q[tt++] = s] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[t];}
int main(){ cin >> n >> m; for(int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); ids.push_back(l[i]), ids.push_back(r[i]); } sort(ids.begin(), ids.end()); ids.erase(unique(ids.begin(), ids.end()), ids.end()); for(int i = 1; i < ids.size(); i++) add1(i, i + 1, m, 0); s = ids.size() + n + 2, t = s + 1; for(int i = 1; i <= n; i++) { add1(0, ids.size() + i, 1, 0), add1(ids.size() + i, ids.size() + n + 1, 1, 0); add1(ids.size() + i, get(l[i]), 1, 0), add1(ids.size() + i, get(r[i]), 1, l[i] - r[i]), ans += r[i] - l[i]; add1(s, ids.size() + i, 1, 0), add1(get(r[i]), t, 1, 0); }
while(ssp()) { ans += flow[t] * dis[t]; for(int i = t; i != s; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t]; } s = 0, t -= 2; while(ssp()) { ans += flow[t] * dis[t]; for(int i = t; i != s; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[t], w[pre[i] ^ 1] += flow[t]; } cout << ans << endl; return 0;}```
## [22.最长k可重线段集问题](https://www.luogu.com.cn/problem/P3357)上面那道的加强版。
不用上面的奇怪解法了,我们来想一下标准解法。首先同样的画出数轴,每一个开区间我们用一条边直接连接首尾,表示选择了它。然后跑一个最大流为 $k$ 的费用流即可。正确性可自行思考或参考题解。
回到此题,注意到,$y$ 这一维实际上影响不大,我们直接把所有线段映射到 $x$ 轴上,即可转化为上一道题。不过可惜的是,这样做只有 $9$ 分,这也是称之加强版的原因。 考虑问题所在。当某条线段的 $x_1=x_2$ 时,他是垂直于 $x$ 轴的,映射过后就成了一个点。根据理论,我们希望这个点为**实心点**(显然其他那些都是空心的,这里的实心空心即为数学上的能否去取到),但是我们上面的方法处理不了实心的点。遂考虑拆点,将一个点变为线段即可。```#include<bits/stdc++.h>using namespace std;#define LL long long
const int N = 2010, M = 5010;int n, m;LL ans;struct node{ int x1, y1, x2, y2; LL len; inline void init() { len = sqrt(((LL)x1 - x2) * ((LL)x1 - x2) + ((LL)y1 - y2) * ((LL)y1 - y2)); }}a[N];vector<int> ids;<https://www.luogu.com.cn/record/141299229>
int h[N], ne[M], e[M], w[M], idx = 1;LL w1[M];void add(int a, int b, int c, LL d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, LL d){ add(a, b, c, d), add(b, a, 0, -d);}
inline int find(int x){ return lower_bound(ids.begin(), ids.end(), x) - ids.begin() + 1;}
int flow[N], pre[N];LL dis[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = m, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[2 * ids.size()];}
int main(){ cin >> n >> m; for(int i = 1; i <= n; i++) { scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2); a[i].init(); ids.push_back(a[i].x1), ids.push_back(a[i].x2); } sort(ids.begin(), ids.end()); ids.erase(unique(ids.begin(), ids.end()), ids.end()); for(int i = 1; i <= n; i++) { int x = find(a[i].x1), y = find(a[i].x2); if(x > y) swap(x, y); if(x == y) add1(2 * x - 1, 2 * x, 1, a[i].len); else add1(2 * x, 2 * y - 1, 1, a[i].len); } for(int i = 1; i <= ids.size(); i++) add1(2 * i - 2, 2 * i - 1, m, 0), add1(2 * i - 1, 2 * i, m, 0); while(ssp()) { ans += dis[2 * ids.size()] * flow[2 * ids.size()]; for(int i = 2 * ids.size(); i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * ids.size()], w[pre[i] ^ 1] += flow[2 * ids.size()]; } cout << ans << endl; return 0;}```
## [23.火星探险问题](https://www.luogu.com.cn/problem/P3356)[P2045 方格取数加强版](https://www.luogu.com.cn/problem/P2045) 这道题挺经典的,同时作为火星探险问题的弱化版,建议先把此题做了。
回到这道题,无非就是不能经过 `1`,经过 `2` 的价值是 $1$。直接拆点即可,将每个点拆为两个,即可控制第一次经过为 $1$,后续经过都为 $0$ 的要求。 当然这个题恶心的是输出方案,直接对于每个机器人在图上跑一下就好了。```#include<bits/stdc++.h>using namespace std;
const int N = 2500, M = 4 * N;int n, m, k;<https://www.luogu.com.cn/record/141218320>
int h[N], ne[M], e[M], w[M], w1[M], idx = 1;void add(int a, int b, int c, int d){ e[++idx] = b, w[idx] = c, w1[idx] = d, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c, int d){ add(a, b, c, d), add(b, a, 0, -d);}
inline int get(int x, int y){ return (x - 1) * m + y;}
int flow[N], dis[N], pre[N];int q[N], hh, tt;bool in[N];bool ssp(){ memset(flow, 0, sizeof(flow)); memset(dis, -0x3f, sizeof(dis)); memset(in, 0, sizeof(in)); hh = tt = 0; flow[0] = k, dis[0] = 0, in[q[tt++] = 0] = 1; while(hh != tt) { int t = q[hh++]; in[t] = 0; if(hh == N) hh = 0; for(int i = h[t]; i; i = ne[i]) if(w[i]) { int j = e[i]; if(dis[j] < dis[t] + w1[i]) { flow[j] = min(flow[t], w[i]), dis[j] = dis[t] + w1[i], pre[j] = i; if(!in[j]) { in[q[tt++] = j] = 1; if(tt == N) tt = 0; } } } } return flow[2 * n * m + 1];}
int main(){ cin >> k >> m >> n; for(int i = 1, x; i <= n; i++) for(int j = 1; j <= m; j++) { scanf("%d", &x); if(x != 1) add1(get(i, j), n * m + get(i, j), 1, !!x), add1(get(i, j), n * m + get(i, j), 1e9, 0); if(i < n) add1(n * m + get(i, j), get(i + 1, j), 1e9, 0); if(j < m) add1(n * m + get(i, j), get(i, j + 1), 1e9, 0); } add1(0, 1, k, 0), add1(2 * n * m, 2 * n * m + 1, k, 0); while(ssp()) { for(int i = 2 * n * m + 1; i; i = e[pre[i] ^ 1]) w[pre[i]] -= flow[2 * n * m + 1], w[pre[i] ^ 1] += flow[2 * n * m + 1]; } for(int i = 1; i <= k; i++) { int x = 1, y = 1, id = 1; while(x != n || y != m) for(int j = h[n * m + id]; j; j = ne[j]) if(w[j ^ 1] && e[j] > id) { w[j]++, w[j ^ 1]--; if(id + 1 == e[j]) printf("%d 1\n", i), y++; else printf("%d 0\n", i), x++; id = e[j]; break; } } return 0;}```
## [24.骑士共存问题](https://www.luogu.com.cn/problem/P3355)题目给了一张图,其实已经算提示了:那张图告诉我们黑白染色过后一定会是一张**二分图**! 于是变为了二分图的最大独立集问题,实现方法很多。```#include<bits/stdc++.h>using namespace std;
const int N = 40010, M = 18 * N, K = 210;int n, m, a[K][K], ans; int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};<https://www.luogu.com.cn/record/141220604>
int h[N], ne[M], e[M], w[M], idx = 1;void add(int a, int b, int c){ e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;}void add1(int a, int b, int c){ add(a, b, c), add(b, a, 0);}
inline int get(int x, int y){ return (x - 1) * n + y;}
int dis[N], cnt[N], cur[N];int sap(int x, int inflow){ if(x == n * n + 1) return inflow; int outflow = 0; for(int i = cur[x]; i; i = ne[i]) if(w[i] && dis[x] == dis[e[i]] + 1) { cur[x] = i; int flow = sap(e[i], min(inflow - outflow, w[i])); w[i] -= flow, w[i ^ 1] += flow, outflow += flow; if(inflow == outflow || dis[0] >= n * n + 2) return outflow; } if(!--cnt[dis[x]]) dis[0] = n * n + 2; cnt[++dis[x]]++, cur[x] = h[x]; return outflow;}
int main(){ cin >> n >> m; for(int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), a[x][y] = 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i + j & 1) { for(int k = 0; k < 8; k++) { int x = i + dx[k], y = j + dy[k]; if(1 <= x && x <= n && 1 <= y && y <= n) add1(get(i, j), get(x, y), 1); } if(!a[i][j]) add1(0, get(i, j), 1); } else if(!a[i][j]) add1(get(i, j), n * n + 1, 1);
for(int i = 0; i <= n * n + 1; i++) cur[i] = h[i]; while(dis[0] < n * n + 2) ans += sap(0, 1e9); cout << n * n - m - ans << endl; return 0;}```
# 八、后记感觉网络流真的非常玄乎,有些题如果不是标签都不会往这方面想。基本靠刷题,见过的多了自然就会了。
参考文献: [浅谈网络流的各种建模方法](https://www.luogu.com.cn/blog/Link-Cut-Treeeee/qian-tan-wang-lao-liu-jian-mu-di-ji-ji-yin-qiao)(好文力荐!)