舞蹈链

舞蹈链(DLX)

算法讲解

在kuangbin第三套里面学习了DLX。
这个东西说明白了就是精确覆盖和重复覆盖的特殊搜索剪枝。所以复杂度贴近暴搜,比赛里面我还没见过用DLX的题目。
具体DLX的原理我这就不写了,给大家推荐个写的好的大佬博客~
个人觉得DLX几乎可以当作黑盒用。只要转化成行坐标是你的所有选择,列坐标所有是你需要覆盖的,每行是每个选择可以覆盖的,就ok了。
这里先给大家贴下我的精确覆盖和重复覆盖的模板

精确覆盖

const int MN = 1005;//最大行数
const int MM = 1005;//最大列数
const int MNN = (int) 1e5 + 5 + MM; //最大点数

struct DLX {
    int n, m, si;
    int U[MNN], D[MNN], L[MNN], R[MNN], Row[MNN], Col[MNN];
    int H[MN], S[MM];
    int ansd, ans[MN];

    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for (int i = 0; i <= m; i++) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;
        L[0] = m;
        si = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void link(int r, int c) {

        ++S[Col[++si] = c];
        Row[si] = r;
        D[si] = D[c];
        U[D[c]] = si;
        U[si] = c;
        D[c] = si;
        if (H[r] < 0)
            H[r] = L[si] = R[si] = si;
        else {
            R[si] = R[H[r]];
            L[R[H[r]]] = si;
            L[si] = H[r];
            R[H[r]] = si;
        }
    }

    void remove(int c) {

        L[R[c]] = L[c];
        R[L[c]] = R[c];
        for (int i = D[c]; i != c; i = D[i])
            for (int j = R[i]; j != i; j = R[j]) {
                U[D[j]] = U[j];
                D[U[j]] = D[j];
                --S[Col[j]];
            }
    }

    void resume(int c) {

        for (int i = U[c]; i != c; i = U[i])
            for (int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }

    bool dance(int d) {

        if (ansd != -1 && ansd < d)return 0;
        if (R[0] == 0) {

            if (ansd == -1)ansd = d;
            else if (d < ansd) ansd = d;
            return 1;
        }
        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;
        remove(c);
        for (int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            for (int j = R[i]; j != i; j = R[j])
                remove(Col[j]);
            (dance(d + 1));
            for (int j = L[i]; j != i; j = L[j])
                resume(Col[j]);
        }
        resume(c);
        return 0;
    }
} dlx;

重复覆盖

const int MN = 55;//最大行数
const int MM = 55;//最大列数
const int MNN = MM * MN + MM + MN + 10; //最大点数

struct DLX {
    int n, m, si;
    int U[MNN], D[MNN], L[MNN], R[MNN], Row[MNN], Col[MNN];
    int H[MN], S[MM];
    int ansd, ans[MN];

    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for (int i = 0; i <= m; i++) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;
        L[0] = m;
        si = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void link(int r, int c) {
        ++S[Col[++si] = c];
        Row[si] = r;
        D[si] = D[c];
        U[D[c]] = si;
        U[si] = c;
        D[c] = si;
        if (H[r] < 0)
            H[r] = L[si] = R[si] = si;
        else {
            R[si] = R[H[r]];
            L[R[H[r]]] = si;
            L[si] = H[r];
            R[H[r]] = si;
        }
    }

    void remove(int c) {
        for (int i = D[c]; i != c; i = D[i]) {
            L[R[i]] = L[i], R[L[i]] = R[i];
        }
    }

    void resume(int c) {
        for (int i = U[c]; i != c; i = U[i]) {
            L[R[i]] = R[L[i]] = i;
        }
    }

    int f_check() {
        int vis[MNN];
        memset(vis, 0, sizeof(vis));
        int ret = 0;
        for (int c = R[0]; c != 0; c = R[c]) vis[c] = true;
        for (int c = R[0]; c != 0; c = R[c])
            if (vis[c]) {
                ret++;
                vis[c] = false;
                for (int i = D[c]; i != c; i = D[i])
                    for (int j = R[i]; j != i; j = R[j])
                        vis[Col[j]] = false;
            }
        return ret;
    }

    bool dance(int d, int limit) {
        if (d > limit)return false;
        if (d + f_check() > limit)return false;
        if (R[0] == 0) {
            ansd = d;
            return true;
        }
        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;
        for (int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            remove(i);
            for (int j = R[i]; j != i; j = R[j])
                remove(j);
            if (dance(d + 1, limit)) return true;
            for (int j = L[i]; j != i; j = L[j])
                resume(j);
            resume(i);
        }
        return false;
    }
} dlx;

题解

我这里面主要就把kuangbin套题前几题的题解稍微写写。

ZOJ-3209 Treasure Map

传送门:ZOJ-3209

题意

给你一个n*m的矩阵,再给你p个小矩阵,告诉你每个矩阵的左下角坐标和右上角坐标,要求你用小矩阵覆盖大矩阵,同时小矩阵不能重叠,求至少用几个矩阵。

题解

把小矩阵的编号作为行,把所有点作为列,这样就转化成了精确覆盖问题了,模板一通xjb套,ac~

ac代码
#include <bits/stdc++.h>

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = (int) 1e6 + 7;
typedef long long ll;

const int MN = 1005;//最大行数
const int MM = 1005;//最大列数
const int MNN = (int) 1e5 + 5 + MM; //最大点数

struct DLX {
    int n, m, si;
    int U[MNN], D[MNN], L[MNN], R[MNN], Row[MNN], Col[MNN];
    int H[MN], S[MM];
    int ansd, ans[MN];

    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for (int i = 0; i <= m; i++) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;
        L[0] = m;
        si = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void link(int r, int c) {

        ++S[Col[++si] = c];
        Row[si] = r;
        D[si] = D[c];
        U[D[c]] = si;
        U[si] = c;
        D[c] = si;
        if (H[r] < 0)
            H[r] = L[si] = R[si] = si;
        else {
            R[si] = R[H[r]];
            L[R[H[r]]] = si;
            L[si] = H[r];
            R[H[r]] = si;
        }
    }

    void remove(int c) {

        L[R[c]] = L[c];
        R[L[c]] = R[c];
        for (int i = D[c]; i != c; i = D[i])
            for (int j = R[i]; j != i; j = R[j]) {
                U[D[j]] = U[j];
                D[U[j]] = D[j];
                --S[Col[j]];
            }
    }

    void resume(int c) {

        for (int i = U[c]; i != c; i = U[i])
            for (int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }

    bool dance(int d) {

        if (ansd != -1 && ansd < d)return 0;
        if (R[0] == 0) {

            if (ansd == -1)ansd = d;
            else if (d < ansd) ansd = d;
            return 1;
        }
        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;
        remove(c);
        for (int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            for (int j = R[i]; j != i; j = R[j])
                remove(Col[j]);
            (dance(d + 1));
            for (int j = L[i]; j != i; j = L[j])
                resume(Col[j]);
        }
        resume(c);
        return 0;
    }
} dlx;


int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int T;
    cin >> T;
    while (T--) {
        int n, m, p;
        cin >> n >> m >> p;
        dlx.init(p, n * m);
        for (int i = 1; i <= p; i++) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            for (int j = x1 + 1; j <= x2; j++) {
                for (int k = y1 + 1; k <= y2; k++) {
                    dlx.link(i, (j - 1) * m + k);
                }
            }
        }
        dlx.ansd = -1;
        dlx.dance(0);
        cout << dlx.ansd << endl;
    }

    return 0;
}

HDU - 2295 Chef and Graph Queries

传送门:HDU-2295

题意

给了n个点和m个圆心,在用不多于k个圆的情况下,使得所有的点被覆盖,最小的圆半径多大

题解

就二分一下答案,判断条件就是用了小于等于k个圆,行为圆,列为点,重复覆盖往上一套,注意一下精度,ac~

ac代码
#include <bits/stdc++.h>

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = (int) 1e6 + 7;
typedef long long ll;
#define clr(x) memset(x,0,sizeof(x));

struct rec {
    int x, y;
};

rec city[maxn], rad[maxn];
double dis[55][55];

const int MN = 55;//最大行数
const int MM = 55;//最大列数
const int MNN = MM * MN + MM + MN + 10; //最大点数

struct DLX {
    int n, m, si;
    int U[MNN], D[MNN], L[MNN], R[MNN], Row[MNN], Col[MNN];
    int H[MN], S[MM];
    int ansd, ans[MN];

    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for (int i = 0; i <= m; i++) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;
        L[0] = m;
        si = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void link(int r, int c) {
        ++S[Col[++si] = c];
        Row[si] = r;
        D[si] = D[c];
        U[D[c]] = si;
        U[si] = c;
        D[c] = si;
        if (H[r] < 0)
            H[r] = L[si] = R[si] = si;
        else {
            R[si] = R[H[r]];
            L[R[H[r]]] = si;
            L[si] = H[r];
            R[H[r]] = si;
        }
    }

    void remove(int c) {
        for (int i = D[c]; i != c; i = D[i]) {
            L[R[i]] = L[i], R[L[i]] = R[i];
        }
    }

    void resume(int c) {
        for (int i = U[c]; i != c; i = U[i]) {
            L[R[i]] = R[L[i]] = i;
        }
    }

    int f_check() {
        int vis[MNN];
        memset(vis, 0, sizeof(vis));
        int ret = 0;
        for (int c = R[0]; c != 0; c = R[c]) vis[c] = true;
        for (int c = R[0]; c != 0; c = R[c])
            if (vis[c]) {
                ret++;
                vis[c] = false;
                for (int i = D[c]; i != c; i = D[i])
                    for (int j = R[i]; j != i; j = R[j])
                        vis[Col[j]] = false;
            }
        return ret;
    }

    bool dance(int d, int limit) {
        if (d > limit)return false;
        if (d + f_check() > limit)return false;
        if (R[0] == 0) {
            ansd = d;
            return true;
        }
        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;
        for (int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            remove(i);
            for (int j = R[i]; j != i; j = R[j])
                remove(j);
            if (dance(d + 1, limit)) return true;
            for (int j = L[i]; j != i; j = L[j])
                resume(j);
            resume(i);
        }
        return false;
    }
} dlx;

int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int T, n, m, k;
    cin >> T;
    while (T--) {
        clr(city);
        clr(rad);
        clr(dis);
        double mx = 0;
        cin >> n >> m >> k;
        for (int i = 1; i <= n; i++) {
            cin >> city[i].x >> city[i].y;
        }
        for (int i = 1; i <= m; i++) {
            cin >> rad[i].x >> rad[i].y;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                double x = 1.0 * city[i].x - rad[j].x;
                double y = 1.0 * city[i].y - rad[j].y;
                dis[i][j] = sqrt(x * x + y * y);
                mx = max(dis[i][j], mx);
            }
        }
        double r = mx * 2;
        double l = 0;
        while (r - l > 1e-8) {
            double mid = (r + l) / 2;
            dlx.init(m, n);
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (dis[i][j] < mid) dlx.link(j, i);
                }
            }
            dlx.ansd = -1;
            if (dlx.dance(0, k)) r = mid;
            else l = mid;
        }
        printf("%.6lf\n", l);
    }

    return 0;
}

FZU - 1686 神龙的难题

传送门:FZU - 1686

题意

中文题题我就不写题意了哈,我相信没有外国人看我博客~

题解

很经典的一道重复覆盖~
和第一题有点像吧,就像第一题一样作,只是别忘了把没有怪物的点去掉就ok了。去掉的办法就是构建一个只有内些没有怪物的点的覆盖,先把这个覆盖放进去,就完成构造了。剩下的构造就和第一题完全一样咯~

ac代码
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <stack>
#include <vector>
#include <iostream>

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = (int) 1e6 + 7;
typedef long long ll;
#define clr(x) memset(x,0,sizeof(x));

struct rec {
    int x, num;
} mp[20][20];


const int MN = 250;//最大行数
const int MM = 250;//最大列数
const int MNN = MM * MN; //最大点数

struct DLX {
    int n, m, si;
    int U[MNN], D[MNN], L[MNN], R[MNN], Row[MNN], Col[MNN];
    int H[MN], S[MM];
    int ansd, ans[MN];

    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for (int i = 0; i <= m; i++) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0;
        L[0] = m;
        si = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void link(int r, int c) {
        ++S[Col[++si] = c];
        Row[si] = r;
        D[si] = D[c];
        U[D[c]] = si;
        U[si] = c;
        D[c] = si;
        if (H[r] < 0)
            H[r] = L[si] = R[si] = si;
        else {
            R[si] = R[H[r]];
            L[R[H[r]]] = si;
            L[si] = H[r];
            R[H[r]] = si;
        }
    }

    void remove(int c) {
        for (int i = D[c]; i != c; i = D[i]) {
            L[R[i]] = L[i], R[L[i]] = R[i];
        }
    }

    void resume(int c) {
        for (int i = U[c]; i != c; i = U[i]) {
            L[R[i]] = R[L[i]] = i;
        }
    }

    bool vis[MNN];
    int f_check() {
        memset(vis, 0, sizeof(vis));
        int ret = 0;
        for (int c = R[0]; c != 0; c = R[c]) vis[c] = true;
        for (int c = R[0]; c != 0; c = R[c])
            if (vis[c]) {
                ret++;
                vis[c] = false;
                for (int i = D[c]; i != c; i = D[i])
                    for (int j = R[i]; j != i; j = R[j])
                        vis[Col[j]] = false;
            }
        return ret;
    }

    void dance(int d) {
        if (d + f_check() >= ansd)return;
        if (R[0] == 0) {
            if (ansd > d) ansd = d;
            return;
        }
        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;
        for (int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            remove(i);
            for (int j = R[i]; j != i; j = R[j])
                remove(j);
            dance(d + 1);
            for (int j = L[i]; j != i; j = L[j])
                resume(j);
            resume(i);
        }
    }
} dlx;

int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int n, m;
    while (cin >> n >> m) {
        clr(mp);
        int tot = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int q;
                cin >> q;
                mp[i][j].x = q;
                if (q == 1) {
                    tot++;
                    mp[i][j].num = tot;
                }
            }
        }
        int x, y;
        cin >> x >> y;
        dlx.init(n * m, tot);
        int num = 0;
        for (int i = 1; i <= n - x + 1; i++) {
            for (int j = 1; j <= m - y + 1; j++) {
                num++;
                for (int r = i; r <= i + x - 1; r++) {
                    for (int c = j; c <= j + y - 1; c++) {
                        if (mp[r][c].x) {
                            dlx.link(num, mp[r][c].num);
                        }
                    }
                }
            }
        }

        dlx.ansd = inf;
        dlx.dance(0);
        cout << dlx.ansd << endl;

    }

    return 0;
}

POJ-1084 Square Destroyer

传送门:POJ-1084

题意

给出一个n*n的大网格,然后它由一系列的火柴拼成,题中给的图片是第二组测试数据。现在我们已经删掉了若干个火柴,问至少还需要删掉多少火柴,才能使图中的若干个正方形全部被破坏。

题解

这个题的建图可是不是一般的恶心,每个正方形都需要有其上的至少一根火柴被选,那么就转化成了DLX模型。每行是一跟火柴,每列是一个正方形,当然正方形个数是<=5*5+4*4+3*3+2*2+1*1=55的,然后显然若某行这根火柴在某列的正方形中,则new一个节点。这样就可以直接套板子就ok了。

ac代码
#include<iostream>
#include<cstring>

using namespace std;

const int INF = 10e8;
const int MaxN = 70;
const int MaxM = 70;
const int MaxNode = MaxN * MaxM;

struct DLX {
    int L[MaxNode], R[MaxNode], U[MaxNode], D[MaxNode], col[MaxNode], row[MaxNode];
    int S[MaxM], H[MaxN];
    int n, m, size;
    int ans;

    void init(int _n, int _m) {
        n = _n;
        m = _m;

        for (int i = 0; i <= m; ++i) {
            U[i] = D[i] = i;
            R[i] = i + 1;
            L[i] = i - 1;
            row[i] = 0;                // !!!

            S[i] = 0;
        }

        R[m] = 0;
        L[0] = m;

        size = m;
        ans = INF;

        for (int i = 0; i <= n; ++i)        // !!!
            H[i] = -1;
    }

    void Link(int r, int c) {
        col[++size] = c;
        ++S[c];
        row[size] = r;

        U[size] = U[c];
        D[size] = c;
        D[U[c]] = size;
        U[c] = size;

        if (H[r] == -1)
            H[r] = L[size] = R[size] = size;
        else {
            L[size] = L[H[r]];
            R[size] = H[r];
            R[L[H[r]]] = size;
            L[H[r]] = size;
        }
    }

    void remove(int c) {
        for (int i = D[c]; i != c; i = D[i]) {
            R[L[i]] = R[i];
            L[R[i]] = L[i];
        }
    }

    void remove1(int r) {
        if (H[r] == -1)
            return;

        for (int i = U[H[r]]; i != H[r]; i = U[i]) {
            if (H[row[i]] == i)            // !!!
            {
                if (R[i] == i)
                    H[row[i]] = -1;
                else
                    H[row[i]] = R[i];
            }

            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }

        for (int i = R[H[r]]; i != H[r]; i = R[i])
            for (int j = U[i]; j != i; j = U[j]) {
                if (H[row[j]] == j) {
                    if (R[j] == j)
                        H[row[j]] = -1;
                    else
                        H[row[j]] = R[j];
                }

                L[R[j]] = L[j];
                R[L[j]] = R[j];
            }
    }

    void resume(int c) {
        for (int i = U[c]; i != c; i = U[i])
            R[L[i]] = L[R[i]] = i;
    }

    bool vis[MaxM];

    int getH() {
        int ret = 0;

        for (int c = R[0]; c != 0; c = R[c])
            vis[c] = 1;

        for (int c = R[0]; c != 0; c = R[c])
            if (vis[c]) {
                ++ret;
                vis[c] = 0;

                for (int i = D[c]; i != c; i = D[i])
                    for (int j = R[i]; j != i; j = R[j])
                        vis[col[j]] = 0;
            }

        return ret;
    }

    void Dance(int d) {
        if (d + getH() >= ans)
            return;

        if (R[0] == 0) {
            if (d < ans)
                ans = d;

            return;
        }

        int c = R[0];

        for (int i = R[0]; i != 0; i = R[i])
            if (S[i] < S[c])
                c = i;

        for (int i = D[c]; i != c; i = D[i]) {
            remove(i);

            for (int j = R[i]; j != i; j = R[j])
                remove(j);

            Dance(d + 1);

            for (int j = L[i]; j != i; j = L[j])
                resume(j);

            resume(i);
        }
    }
}dlx;

int N;
int ans1[6] = {0, 1, 3, 6, 9, 14};

void slove() {
    dlx.init(2 * N * (N + 1), N * (N + 1) * (2 * N + 1) / 6);

    int t1, t2;
    int cou = 0;
    int K, a;

    cin >> K;

    if (K == 0) {
        cout << ans1[N] << endl;
        return;
    }

    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= (N - i + 1) * (N - i + 1); ++j) {
            ++cou;
            t1 = (j - 1) % (N - i + 1) + 1 + (2 * N + 1) * ((j - 1) / (N - i + 1));
            for (int k = 0; k < i; ++k) {
                dlx.Link(k + t1, cou);
                dlx.Link((2 * N + 1) * k + t1 + i - 1 + N + 1, cou);
                dlx.Link((2 * N + 1) * k + t1 + N, cou);
                dlx.Link(k + t1 + i * (2 * N + 1), cou);
            }
        }

    for (int i = 0; i < K; ++i) {
        cin >> a;
        dlx.remove1(a);
    }

    dlx.Dance(0);

    if (dlx.ans == INF)
        cout << 0 << endl;
    else
        cout << dlx.ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--) {
        cin >> N;

        slove();
    }
    return 0;
}

小计

自己也是真懒,隔了这么久才写博客~大家捧场啦~~~

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值