棋盘上的守卫

一.前言

这个题好 妙啊,不写标签我是真没想到。。。

二.题面

棋盘上的守卫

三.题解部分

我们思考一个问题,选择一个点 a [ i ] [ j ] a[i][j] a[i][j] T a Ta Ta 能为我们带来什么呢???

这里定义一个阶段的概念:阶段 i i i 表示横向或纵向前 i − 1 i - 1 i1 (列/行)已经安排好守卫,开始放置第 i i i (列/行)。

( i , j ) (i,j) (i,j) 这个点上我们可以放置两种守卫,第一种是横向守卫,第二种是竖向守卫,所以 T a Ta Ta 们之间只能选择一种,可以抽象成一条边,链接的是横向的第 i i i 个阶段,竖向的第 j j j 个阶段,为了方便,我们将 j j j 的下标写作 j + n j + n j+n

可以得到一条性质: 对于任意一个点 i i i, 若 i > n i>n i>n,则这是列的阶段

attention:将其抽象成图后,边 ( i , j , v a l ) (i, j, val) (i,j,val) 表示由 i i i 阶段转到 j j j 阶段的花费为 v a l val val

所以我们的答案为原图的一个子图,满足子图为一颗基环树.

举个例子,如下面的样例

2 2
1 3
2 1

在这里插入图片描述
70 p t s 70pts 70pts

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

// #define int long long
#define LL long long

template <typename T> int read (T &x) {x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return 1;}
template <typename T> void write (T x) {if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
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; }

const int Maxn = 1e6;

int n, m;
LL ans;

int tot;
struct Node {
    int x, y;
    LL val;
}sec[Maxn + 5];
bool operator < (Node x, Node y) {
    return x.val < y.val;
}

int fa[Maxn + 5];
void MakeSet () {
    for (int i = 0; i <= Maxn; i++) {
        fa[i] = i;
    }
}
int FindSet (int x) {
    if (x != fa[x]) {
        fa[x] = FindSet (fa[x]);
    }
    return fa[x];
}
bool UnionSet (int x, int y) {
    int u = FindSet (x), v = FindSet (y);
    if (u == v) return 0;
    fa[u] = v;
    return 1;
}
void Kruskal () {
    sort (sec + 1, sec + 1 + tot);
    bool flag = 0;
    for (int i = 1; i <= tot; i++) {
        if (UnionSet (sec[i].x, sec[i].y)) {
            ans += sec[i].val;
        }
        else {
            if (flag) continue;
            flag = 1;
            ans += sec[i].val;
        }
    }
}

signed main () {
    MakeSet ();
    read (n); read (m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            LL w; read (w);
            sec[++tot].x = i; sec[tot].y = j + n; sec[tot].val = w;
        }
    
    Kruskal ();
    write (ans);
	return 0;
}

W A WA WA 了?? 最开始我也很不解,后来仔细思考了一下:我们并不一定要保证图联通,所以子图为基环树森林也可以。

所以很容易构造出 h a c k hack hack 数据
.in

4 4
1 1 9 9
1 1 9 9
9 9 1 1
9 9 1 1

.out

8

(竟然还能有70pts,数据好水啊)

s t d std std

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

// #define int long long
#define LL long long

template <typename T> int read (T &x) {x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return 1;}
template <typename T> void write (T x) {if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
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; }

const int Maxn = 1e5;

int n, m;
LL ans;

int tot;
struct Node {
    int x, y;
    LL val;
}sec[Maxn + 5];
bool operator < (Node x, Node y) {
    return x.val < y.val;
}

int fa[Maxn + 5];
bool vis[Maxn + 5];
void MakeSet () {
    for (int i = 0; i <= Maxn; i++) {
        fa[i] = i;
    }
}
int FindSet (int x) {
    if (x != fa[x]) {
        fa[x] = FindSet (fa[x]);
    }
    return fa[x];
}
void Kruskal () {
    sort (sec + 1, sec + 1 + tot);
    bool flag = 0;
    for (int i = 1; i <= tot; i++) {
        int u = FindSet (sec[i].x), v = FindSet (sec[i].y);
        if (vis[u] == 1 && vis[v] == 1) {
            continue;
        }
        else if (u == v) {
            vis[u] = 1;
            ans += sec[i].val;
        }
        else {
            fa[u] = v;
            vis[v] |= vis[u];
            ans += sec[i].val;
        }
    }
}

signed main () {
    MakeSet ();
    read (n); read (m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            LL w; read (w);
            sec[++tot].x = i; sec[tot].y = j + n; sec[tot].val = w;
        }
    
    Kruskal ();
    write (ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值