【学习笔记】最小割

ps:代码见最后

定义:

割集:再一个有向图 ( V , E ) (V, E) (V,E) 上,将所有点分成两个集合 S , T ( T = V − S ) S, T (T = V - S) S,T(T=VS),则这种选择割集的方式 f f f 记作 ( S , T ) (S, T) (S,T)

割集的权值:在有向图上,分出两个点集 S , T S, T S,T ∣ f ∣ = ∑ s ∈ S , t ∈ T c [ s ] [ t ] |f| = \sum_{s \in S, t \in T} c[s][t] f=sS,tTc[s][t] (注意是有向图,且权值算的是边的容量而不是流量)



求法

最小割等于最大流,所以计算最小割就是计算最大流。

理解:
在这里插入图片描述

相当于我们有许多条 s ⇝ t s \rightsquigarrow t st,为了让 s , t s, t s,t 不连通,我们就要把这两个点之间的路径全部切断 (当然也有可能有两条路径有交叉,图上有这样的路径),这时候我们发现最小割可以翻译为:通过最少的流量阻塞所有 s ⇝ t s \rightsquigarrow t st ,那么就是要阻塞所有 s ⇝ t s \rightsquigarrow t st 的增广路(因为没有不为零的边联通 s , t s, t s,t,那么就表示不存在增广路),即是最大流。

注: 这只是便于记忆的感性证明(虽然听起来也没有漏洞),理性证明可以参考算法导论。

我们就可以用这个感性理解去做很多题了,不然理性证明会很麻烦,下面举两个例子就可以说明这个感性理解有多么有用了。



例题

例题题解(不过题解也摆在下面了,不想看到太多内容就分开了):
0.
1.
2.
3.


题意:给出一个图,问最少删多少个边使得图不连通。

枚举源点和汇点,相当于让这个图拆成两个点集,去除这两个点集之间的边的最小代价,发现这就是最小割。

发现可以不枚举源点。

证明:

假设最优解为 ( A S , A T ) (AS,AT) (AS,AT)

考虑点一,不妨假设它在 A S AS AS 里。

那么通过枚举 t = 2 t = 2 t=2 ~ n n n,我们必然会选到一个点属于 A T AT AT 集合,这时最小割求出的就是答案了。

我只证明了可以通过只枚举汇点就能得到答案(充分性),没有证明这样枚举能包含所有情况(必要性),但是我们只要充分性就行了,这样就一定能正确求解(@DJ)。


  1. SP300 CABLETV - Cable TV Network

题意:给出一个图,问最少删多少个点使得图不连通。

考虑转换为 0. 0. 0.

我们可以想到拆点:

在这里插入图片描述

把原图中点 i i i 去掉就先当于,在新图中把 i → i ′ i \rightarrow i' ii 删掉了,这样通过 i i i 的路径就不复存在了。

我们用 0. 0. 0. 的套路,枚举 u , v u, v u,v 不连通。

构造好了,我们来看看怎么从原图转化到新构造的图,结论就是不存在 u ′ ⇝ v u' \rightsquigarrow v uv

理解:

如图,例如存在 u ⇝ v u \rightsquigarrow v uv 的路径为如下图
在这里插入图片描述

相当于我们要阻塞这条增广路,但是我们发现如果这样增广的话,可以选择将 u → u ′ , v → v ′ u \rightarrow u', v \rightarrow v' uu,vv 两边删去,这样的话就不满足要求了(即在原图中删掉了我们钦定的不在同一联通块的点),所以我们选择 u ′ , v u', v u,v 作源汇点。

但是还有一种情况,它饶了一圈然后经过了 u → u ′ u \rightarrow u' uu, v → v ′ v \rightarrow v' vv 呢?,这个时候分两种情况:

一、这个路径上存在一条非 u → u ′ u \rightarrow u' uu, v → v ′ v \rightarrow v' vv 的路径,那么我们选择删去这条边而不选择 u → u ′ u \rightarrow u' uu 就行了(当然我们实际打代码时不需要管这条增广路到底删去了哪条边,只要知道它一定能构造出来就行了)。

二、这个路径上没有非 u → u ′ u \rightarrow u' uu, v → v ′ v \rightarrow v' vv 的路径 (即只有 u ′ → v u' \rightarrow v uv

在这里插入图片描述

这个时候增广路为 + ∞ +\infty +,我们就可以不用特判了。

所以其实最后代码非常简单,但是思维难度还是挺大的。(逻辑链条短,但是思维很非人……)


  1. 文理分科

ps:由于画图不好画有向边,这里的边的方向都是从左(源点)到右(汇点), 看不懂也没办法了。

首先把找最大贡献改为去掉最少的贡献。

还是先用最小割的套路,连接 s s s 的边断了表示选择理科,连接 t t t 的边断了表示选择文科。

所以 s → a s \rightarrow a sa 的边权为 a r t [ a ] art[a] art[a] (选择理科就没有选择文科的贡献), a → t a \rightarrow t at 的边权为 s c i e n c e [ a ] science[a] science[a](同理)。

如图(a[1~4]表示a的相邻点)。

在这里插入图片描述
现在考虑怎么连边可以处理需要去掉全为理科(或文科)的贡献。

拿全选理科的贡献举例。

相当于当有一条 a[0~4] → \rightarrow t的边没有被断掉,那么表示五个人中有人选择了文科,这时就需要减去全选理科的贡献。

考虑如下图的连边。

在这里插入图片描述

如果没断掉 a [ i ] → t a[i] \rightarrow t a[i]t,那么相当于 a [ i ] a[i] a[i] 选择了文科,那么这时有两条增广路 s → a [ i ] → a ′ → t s \rightarrow a[i] \rightarrow a' \rightarrow t sa[i]at s → a ′ ′ → a → a ′ → t s \rightarrow a'' \rightarrow a \rightarrow a' \rightarrow t saaat,为了破坏这两条增广路,我们发现只能选择

  1. a ′ → t a' \rightarrow t at,这种情况的贡献为 same_science[a]
  2. s → a 和 s → a ′ s \rightarrow a 和 s \rightarrow a' sasa,这种情况的贡献为 same_art[a],art[a],可是总贡献为 same_art[a],art[a],science[a]。不如 art[a],same_art[a],所以(最小割)肯定不会选择这样断边。
  3. 其他的边,都为 Inf 肯定不选了。

所以这个构造满足了所有条件(妙啊。

代码非常非常简单……麻了


3.老C的方块

我们发现可以把整个图根据特殊边为中心分成许多可重复的 p a r t part part,如图所示。

在这里插入图片描述

(直接mao了洛谷的图例,侵删

我们考虑一种不合法的方案,它的关键边必定是某个 p a r t part part 中的关键边,所以我们只要保证每个 p a r t part part 内部都是合法方案就行了。

考虑这样一张图(如上图),我们发现,满足要求有四种方案:

  1. 删除所有红块
  2. 删除所有绿块
  3. 删除黑块
  4. 删除橙块

在这里插入图片描述

那么,我们就把

  1. 所有的红块分在一部图

  2. 所有的绿块分在一部图

  3. 黑块分在一部图

  4. 橙块分在一部图

如图所示

在这里插入图片描述

这样连图就保证了四种断边方法,以及其对应的代价。

如果思路正确还挺好写的……

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
    x = 0; T f = 1;
    char ch = getchar ();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar ();
    }
    x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
    read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
    if (x == 0) { putchar ('0'); return; }
    if (x < 0) { putchar ('-'); x = -x; }
    int poi = 0;
    while (x) {
        For_Print[++poi] = x % 10 + '0';
        x /= 10;
    }
    while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
    write (x); putchar (ch);
}

const LL Mod = 1e9 + 7;
const int Maxn = 2 * 1e5;
const int Maxm = 1e7;
const int Maxt = 1e5;
const int Maxd = 2;
const LL Inf = 0x3f3f3f3f;

void del (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void add (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }

int s, t;

// map <int, PII> debug;
// void inout (int x) {
//     if (x == s) printf ("s");
//     else if (x == t) printf ("t");
//     else printf ("(%d,%d)", debug[x].fi, debug[x].se);
// }

set <PII> vis;
struct edge {
    int to[Maxm * 2 + 5], Next[Maxm * 2 + 5]; LL val[Maxm * 2 + 5];
    int len, Head[Maxn + 5];
    edge () { len = 1; memset (Head, 0, sizeof Head); }
    void Init () {
        len = 1;
        memset (Head, 0, sizeof Head);
    }
    void plus (int x, int y, LL w) {
        to[++len] = y;
        Next[len] = Head[x];
        val[len] = w;
        Head[x] = len;
    }
    void add (int x, int y, LL w) {

        // inout (x); putchar (' '); inout (y); putchar (' '); print (w, '\n');
        if (vis.find (MP (x, y)) != vis.end ()) return;
        vis.insert (MP (x, y));

        plus (x, y, w); plus (y, x, 0);
    }
    void rev_add (int x, int y, LL w) {
        plus (y, x, w); plus (x, y, 0);
    }
}mp;

int hh, tt, q[Maxn + 5];
int depth[Maxn + 5], cur[Maxn + 5];
bool BFS () {
    rep (i, 1, tt) depth[q[i]] = 0;
    hh = 1; tt = 0; q[++tt] = s;
    depth[s] = 1; cur[s] = mp.Head[s];
    while (hh <= tt) {
        int u = q[hh++];
        for (int i = mp.Head[u]; i; i = mp.Next[i]) {
            int v = mp.to[i]; LL w = mp.val[i];
            if (w == 0) continue;
            if (depth[v]) continue;
            depth[v] = depth[u] + 1;
            cur[v] = mp.Head[v];
            q[++tt] = v;
            if (v == t) return 1;
        }
    }
    return 0;
}
LL DFS (int u, LL Up) {
    if (u == t) return Up;
    if (Up == 0) return 0;
    LL flow = 0;
    while (cur[u] && flow < Up) {
        int i = cur[u]; cur[u] = mp.Next[cur[u]];
        int v = mp.to[i]; LL w = mp.val[i];
        if (w == 0) continue;
        if (depth[v] != depth[u] + 1) continue;
        LL tmp = DFS (v, Min (Up - flow, w));
        if (tmp == 0) depth[v] = -1;
        flow += tmp; mp.val[i] -= tmp; mp.val[i ^ 1] += tmp;
        if (mp.val[i] && flow >= Up) cur[u] = i;
    }
    return flow;
}
LL Dinic () {
    LL flow = 0;
    while (BFS ()) {
        flow += DFS (s, Inf);
    }
    return flow;
}

int c, r, n;

struct Node {
    int x, y, w;
}a[Maxn + 5];
bool is_in (int x, int y, int nx, int ny) {
    if (x - 1 <= nx && nx <= x + 2 && y - 1 <= ny && ny <= y + 1) return 1;
    else return 0;
}
bool check (int x, int y) {
    if (y & 1) return x % 4 == 1;
    else return x % 4 == 3;
}

int colour[2][10] = {
    { 0, 1, 2, 1, 3, 4, 2, 1, 2 },
    { 0, 2, 1, 2, 4, 3, 1, 2, 1 }
};
int tox[15] = { 0, 0, 1, -1, 0, 1, 2, 0, 1 };
int toy[15] = { 0, 1, 1, 0, 0, 0, 0, -1, -1 };

int cnt_Hash;
map <PII, int> Hash;
int Get (int x, int y) {

    // if (Hash.find (MP (x, y)) == Hash.end ())
    //     debug[cnt_Hash + 1] = MP (x, y);

    if (Hash.find (MP (x, y)) == Hash.end ())
        Hash[MP (x, y)] = ++cnt_Hash;
    return Hash[MP (x, y)];
}
set <PII> used;
map <PII, int> val;
void Build (int x, int y) {
    if (used.find (MP (x, y)) != used.end ())
        return;
    if (val.find (MP (x, y)) == val.end ()) return;
    if (val.find (MP (x + 1, y)) == val.end ()) return;
    used.insert (MP (x, y));
    rep (i, 1, 8) {
        int nx = x + tox[i], ny = y + toy[i];
        if (val.find (MP (nx, ny)) == val.end ()) continue;

        // if (nx == 1 && ny == 2) 
        //     printf ("I Love DSY\n");

        if (colour[y & 1][i] == 1) 
            mp.add (s, Get (nx, ny), val[MP (nx, ny)]);
        if (colour[y & 1][i] == 2) 
            mp.add (Get (nx, ny), t, val[MP (nx, ny)]);
        rep (j, 1, 8) {
            int nnx = x + tox[j], nny = y + toy[j];
            if (val.find (MP (nnx, nny)) == val.end ()) continue;

            if (colour[y & 1][i] == 1 && colour[y & 1][j] == 3) mp.add (Get (nx, ny), Get (nnx, nny), val[MP (nx, ny)]);
            if (colour[y & 1][i] == 3 && colour[y & 1][j] == 4) mp.add (Get (nx, ny), Get (nnx, nny), Min (val[MP (nx, ny)], val[MP (nnx, nny)]));
            if (colour[y & 1][i] == 4 && colour[y & 1][j] == 2) mp.add (Get (nx, ny), Get (nnx, nny), val[MP (nx, ny)]);
        }
    }
}

int main () {
    // freopen ("D:\\lihan\\1.in", "r", stdin);
    // freopen ("D:\\lihan\\1.out", "w", stdout);
    
    s = Maxn - 2; t = Maxn - 1;

    read (c, r, n);
    rep (i, 1, n) {
        read (a[i].x, a[i].y, a[i].w);
        val[MP (a[i].x, a[i].y)] = a[i].w;
    }

    rep (i, 1, n) {
        int x = a[i].x, y = a[i].y;
        rep (dx, -Maxd, Maxd) {
            rep (dy, -Maxd, Maxd) {
                int nx = x + dx, ny = y + dy;
                if (!check (nx, ny)) continue;
                if (is_in (nx, ny, x, y))
                    Build (nx, ny);
            }
        }
    }

    print (Dinic (), '\n');
    return 0;
}


code:

SP300 CABLETV - Cable TV Network

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
	x = 0; T f = 1;
	char ch = getchar ();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar ();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar ();
	}
	x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
	read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
	if (x == 0) { putchar ('0'); return; }
	if (x < 0) { putchar ('-'); x = -x; }
	int poi = 0;
	while (x) {
		For_Print[++poi] = x % 10 + '0';
		x /= 10;
	}
	while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
	write (x); putchar (ch);
}

const LL Mod = 1e9 + 7;
const int Maxn = 2 * 1e3;
const int Maxm = 1e5;
const LL Inf = 0x3f3f3f3f;

void del (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void add (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }

struct edge {
    int to[Maxm * 2 + 5], Next[Maxm * 2 + 5]; LL val[Maxm * 2 + 5];
    int len, Head[Maxn + 5];
    edge () { len = 1; memset (Head, 0, sizeof Head); }
    void Init () {
        len = 1;
        memset (Head, 0, sizeof Head);
    }
    void plus (int x, int y, LL w) {
        to[++len] = y;
        Next[len] = Head[x];
        val[len] = w;
        Head[x] = len;
    }
    void add (int x, int y, LL w) {
    	plus (x, y, w); plus (y, x, 0);
	}
}mp;

int s, t;
int hh, tt, q[Maxn + 5];
int depth[Maxn + 5], cur[Maxn + 5];
bool BFS () {
	rep (i, 1, tt) depth[q[i]] = 0;
    hh = 1; tt = 0; q[++tt] = s;
	depth[s] = 1; cur[s] = mp.Head[s];
    while (hh <= tt) {
        int u = q[hh++];
        for (int i = mp.Head[u]; i; i = mp.Next[i]) {
            int v = mp.to[i]; LL w = mp.val[i];
            if (w == 0) continue;
            if (depth[v]) continue;
            depth[v] = depth[u] + 1;
            cur[v] = mp.Head[v];
            q[++tt] = v;
            if (v == t) return 1;
        }
    }
    return 0;
}
LL DFS (int u, LL Up) {
    if (u == t) return Up;
    if (Up == 0) return 0;
    LL flow = 0;
    for (int i = cur[u]; i && Up != flow; i = mp.Next[i]) {
        int v = mp.to[i]; LL w = mp.val[i];
        cur[u] = i;
        if (w == 0) continue;
        if (depth[v] != depth[u] + 1) continue;
        LL tmp = DFS (v, Min (Up - flow, w));
        if (tmp == 0) depth[v] = -1;
        flow += tmp; mp.val[i] -= tmp; mp.val[i ^ 1] += tmp;
    }
    return flow;
}
LL Dinic () {
	LL flow = 0;
	while (BFS ()) {
		flow += DFS (s, Inf);
	}
	return flow;
}

int n, m;
int x[Maxn + 5], y[Maxn + 5];

int main () {
	// freopen ("D:\\lihan\\1.in", "r", stdin);
	// freopen ("D:\\lihan\\1.out", "w", stdout);

    while (cin >> n >> m) {
        rep (i, 1, m) read (x[i], y[i]), x[i]++, y[i]++;

        LL ans = n;
        rep (date_t, 2, n) {
            mp.Init ();

            s = 1 + n; t = date_t;

            rep (i, 1, n) mp.add (i, i + n, 1);
            rep (i, 1, m) {
                mp.add (x[i] + n, y[i], 1);
                mp.add (y[i] + n, x[i], 1);
            }
            ans = Min (ans, Dinic ());
        }
        if (ans == n - 1) {
            print (n, '\n');
        }
        else {
            print (ans, '\n');
        }
    }
	return 0;
}

文理分科

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
    x = 0; T f = 1;
    char ch = getchar ();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar ();
    }
    x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
    read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
    if (x == 0) { putchar ('0'); return; }
    if (x < 0) { putchar ('-'); x = -x; }
    int poi = 0;
    while (x) {
        For_Print[++poi] = x % 10 + '0';
        x /= 10;
    }
    while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
    write (x); putchar (ch);
}

const LL Mod = 1e9 + 7;
const int Maxt = 100;
const int Maxn = 2 * 1e5;
const int Maxm = 1e7;
const LL Inf = 0x3f3f3f3f;

void del (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void add (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }

struct edge {
    int to[Maxm * 2 + 5], Next[Maxm * 2 + 5]; LL val[Maxm * 2 + 5];
    int len, Head[Maxn + 5];
    edge () { len = 1; memset (Head, 0, sizeof Head); }
    void Init () {
        len = 1;
        memset (Head, 0, sizeof Head);
    }
    void plus (int x, int y, LL w) {
        to[++len] = y;
        Next[len] = Head[x];
        val[len] = w;
        Head[x] = len;
    }
    void add (int x, int y, LL w) {
        plus (x, y, w); plus (y, x, 0);
    }
}mp;

int s, t;
int hh, tt, q[Maxn + 5];
int depth[Maxn + 5], cur[Maxn + 5];
bool BFS () {
    rep (i, 1, tt) depth[q[i]] = 0;
    hh = 1; tt = 0; q[++tt] = s;
    depth[s] = 1; cur[s] = mp.Head[s];
    while (hh <= tt) {
        int u = q[hh++];
        for (int i = mp.Head[u]; i; i = mp.Next[i]) {
            int v = mp.to[i]; LL w = mp.val[i];
            if (w == 0) continue;
            if (depth[v]) continue;
            depth[v] = depth[u] + 1;
            cur[v] = mp.Head[v];
            q[++tt] = v;
            if (v == t) return 1;
        }
    }
    return 0;
}
LL DFS (int u, LL Up) {
    if (u == t) return Up;
    if (Up == 0) return 0;
    LL flow = 0;
    for (int i = cur[u]; i && Up != flow; i = mp.Next[i]) {
        int v = mp.to[i]; LL w = mp.val[i];
        cur[u] = i;
        if (w == 0) continue;
        if (depth[v] != depth[u] + 1) continue;
        LL tmp = DFS (v, Min (Up - flow, w));
        if (tmp == 0) depth[v] = -1;
        flow += tmp; mp.val[i] -= tmp; mp.val[i ^ 1] += tmp;
    }
    return flow;
}
LL Dinic () {
    LL flow = 0;
    while (BFS ()) {
        flow += DFS (s, Inf);
    }
    return flow;
}

int n, m;
int art[Maxt + 5][Maxt + 5];
int science[Maxt + 5][Maxt + 5];
int same_art[Maxt + 5][Maxt + 5];
int same_science[Maxt + 5][Maxt + 5];
int tox[10] = { 0, 1, -1, 0, 0, 0 };
int toy[10] = { 0, 0, 0, 1, -1, 0 };

int Hash (int x, int y) {
    return (x - 1) * m + y;
}
bool check (int x, int y) {
    if (x < 1 || x > n) return 0;
    if (y < 1 || y > m) return 0;
    return 1;
}

int main () {
    // freopen ("D:\\lihan\\1.in", "r", stdin);
    // freopen ("D:\\lihan\\1.out", "w", stdout);
    read (n, m);
    int tot = 0;
    rep (i, 1, n) rep (j, 1, m) read (art[i][j]), tot += art[i][j];
    rep (i, 1, n) rep (j, 1, m) read (science[i][j]), tot += science[i][j];
    rep (i, 1, n) rep (j, 1, m) read (same_art[i][j]), tot += same_art[i][j];
    rep (i, 1, n) rep (j, 1, m) read (same_science[i][j]), tot += same_science[i][j];

    s = 0; t = Maxn - 1;
    rep (i, 1, n) {
        rep (j, 1, m) {
            mp.add (s, Hash (i, j), art[i][j]);
            mp.add (Hash (i, j), t, science[i][j]);
            mp.add (s, Hash (i, j) + n * m, same_art[i][j]);
            mp.add (Hash (i, j) + n * m * 2, t, same_science[i][j]);
            rep (k, 1, 5) {
                int x = i + tox[k], y = j + toy[k];
                if (!check (x, y)) continue;
                mp.add (Hash (i, j) + n * m, Hash (x, y), Inf);
                mp.add (Hash (x, y), Hash (i, j) + n * m * 2, Inf);
            }
        }
    }

    print (tot - Dinic (), '\n');
    return 0;
}


小结:

eg.

我们经常会遇到:只要出现一个不满足要求的就算贡献,这种情况我们经常利用染色然后如下图建图。

比如我们见了一个虚点,要求不能全部同时出现。

在这里插入图片描述
然后我们可以把图染色分成多部图后改成这样的图。

在这里插入图片描述

posted @ 2021-12-04 11:33  C2022lihan  阅读(79)  评论(0编辑  收藏  举报