CodeForces1082G Petya and Graph 最小割
生活随笔
收集整理的這篇文章主要介紹了
CodeForces1082G Petya and Graph 最小割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網絡流裸題
\(s\)向點連邊\((s, i, a[i])\)
給每個邊建一個點
邊\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及邊\((E, t, w)\)
最小割建模...
然后就沒了....復習一下板子吧
#include <cstdio> #include <cstring> #include <iostream> using namespace std;#define ll long long #define ri register int #define rep(io, st, ed) for(ri io = st; io <= ed; io ++) #define drep(io, ed, st) for(ri io = ed; io >= st; io --)#define gc getchar inline int read() {int p = 0, w = 1; char c = gc();while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();return p * w; }const int sid = 5e4 + 5; const ll inf = 1e17;int n, m, s, t, ip, cnp = 1; int a[sid], q[sid], go[sid], num[sid], L[sid]; int cap[sid], nxt[sid], node[sid]; ll res[sid];inline void addedge(int u, int v, ll w) {nxt[++ cnp] = cap[u]; cap[u] = cnp; node[cnp] = v; res[cnp] = w;nxt[++ cnp] = cap[v]; cap[v] = cnp; node[cnp] = u; res[cnp] = 0; }#define cur node[i] inline void bfs() {int fr = 1, to = 0;L[t] = 1; num[1] ++; q[++ to] = t;while(fr <= to) {int o = q[fr ++];for(int i = cap[o]; i; i = nxt[i])if(res[i ^ 1] && !L[cur]) {q[++ to] = cur;L[cur] = L[o] + 1; num[L[cur]] ++;}}for(int i = 1; i <= t; i ++) go[i] = cap[i]; }inline ll dfs(int o, ll flow) {ll tt = 0, tmp;if(o == t) return flow;for(int &i = go[o]; i; i = nxt[i]) {if(L[cur] + 1 != L[o]) continue;tmp = dfs(cur, min(flow, res[i]));res[i] -= tmp; res[i ^ 1] += tmp;flow -= tmp; tt += tmp;if(!flow) return tt;}-- num[L[o]]; if(!num[L[o]]) L[s] = t + 1;L[o] ++; num[L[o]] ++;go[o] = cap[o];return tt; }inline ll isap(int s, int t) {bfs();ll ret = dfs(s, inf);while(L[s] <= t) ret += dfs(s, inf);return ret; }int main() {ll ans = 0;n = read(); m = read();rep(i, 1, n) a[i] = read();s = n + m + 1; t = s + 1;rep(i, 1, n) addedge(s, i, a[i]);rep(i, 1, m) {int u = read(), v = read(), w = read();addedge(u, n + i, inf); addedge(v, n + i, inf);addedge(n + i, t, w); ans += w;}ans -= isap(s, t);printf("%lld\n", ans);return 0; }
轉載于:https://www.cnblogs.com/reverymoon/p/10164746.html
總結
以上是生活随笔為你收集整理的CodeForces1082G Petya and Graph 最小割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 92.反转链表
- 下一篇: Windows Defender还原误删