老C的方块题解

裁剪自我的blog

好题

我们发现可以把整个图根据特殊边为中心分成许多可重复的 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;
}
posted @ 2021-12-10 14:18  C2022lihan  阅读(34)  评论(0编辑  收藏  举报