[NOI2014] 魔法森林

Portal

对于这一题, 我们考虑直接求出路径是非常麻烦的.

那么采用一个枚举答案的办法, 因为取值范围有限, 我们直接枚举边即可.

我们枚举\(A\)的取值, 直接维护另一边的\(B\).

考虑钦定的这条边一定要被选. 那么小于这条边的权值的边只要保证\(1\), \(N\)两者联通就可以了.

于是我们对另一边维护一个最小生成树.

这个可以把边单独建成LCT中的点, 然后点的权为0, 边的权为边权. 然后直接维护即可.

其实这题就是一个套路题, 在有两种元素的情况下(比如这题/坐标), 我们可以枚举1维度, 用东西去维护另一个维度, 这样一定能遍历出所有解.

Code

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
int read() {
    char ch = getchar();
    int x = 0, flag = 1;
    for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
    for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    return x * flag;
}
void write(int x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) write(x / 10);
    putchar(x % 10 + 48);
}

const int Maxn = 50009, Maxm = 100009, Maxv = 50009;

struct edge {
    int u, v, a, b;
    bool operator < (const edge &Another) const {
        return a < Another.a;
    }
};

template <int N> struct LCT {
    struct node {
        int fa, ch[2], revTag;
        pair<int, int> sumVal; int val;
    }t[N];
    int amt, _top, stk[N];

#define fa(x) t[(x)].fa
#define lc(x) t[(x)].ch[0]
#define rc(x) t[(x)].ch[1]

    int isroot(int u) { return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u; }
    void pushup(int u) {
        t[u].sumVal = max(make_pair(t[u].val, u), max(t[lc(u)].sumVal, t[rc(u)].sumVal));
    }
    void setRev(int u) {
        t[u].revTag ^= 1;
        swap(lc(u), rc(u));
    }
    void pushdown(int u) {
        if (t[u].revTag) {
            t[u].revTag = 0;
            setRev(lc(u)), setRev(rc(u));
        }
    }

    void rotate(int u) {
        int y = fa(u), z = fa(y), dir = (rc(y) == u);
        if (!isroot(y)) t[z].ch[rc(z) == y] = u; t[u].fa = z;
        t[y].ch[dir] = t[u].ch[dir ^ 1]; t[t[u].ch[dir ^ 1]].fa = y;
        t[u].ch[dir ^ 1] = y; t[y].fa = u;
        pushup(y), pushup(u);
    }
    void splay(int u) {
        stk[_top = 1] = u;
        for (int jwb = u; !isroot(jwb); jwb = t[jwb].fa) stk[++_top] = t[jwb].fa;
        while (_top) pushdown(stk[_top--]);
        while (!isroot(u)) {
            int y = t[u].fa, z = t[y].fa;
            if (!isroot(y)) 
                (t[z].ch[1] == y) ^ (t[y].ch[1] == u) ? rotate(u) : rotate(y);
            rotate(u);
        }
        pushup(u);
    }

    void access(int u) {
        for (int y = 0; u; u = fa(y = u)) 
            splay(u), t[u].ch[1] = y, pushup(u);
    }
    void makeRoot(int u) {
        access(u), splay(u), setRev(u);
    }
    int findRoot(int u) {
        access(u);  splay(u);
        while (lc(u)) pushdown(u), u = lc(u);
        return u;
    }   
    void link(int u, int v) { makeRoot(u); splay(u); t[u].fa = v; }
    void cut(int u, int v) {
        makeRoot(u); access(v); splay(v);
        if (findRoot(v) == u && t[u].fa == v && t[v].ch[0] == u) {
            t[u].fa = t[v].ch[0] = 0;
            pushup(v);
        }
    }
    pair<int, int> split(int u, int v) {
        makeRoot(u); access(v); splay(v);
        return t[v].sumVal;
    }
    int newnode(int val, int pa = 0) {
        int res = ++amt;
        t[res].val = val;
        t[res].sumVal = make_pair(val, res);
        t[res].fa = pa;
        return res;
    }
#undef fa
#undef lc
#undef rc
};

template <int N> struct DSU {
    int fa[N];

    void init() { rep (i, 0, N - 1) fa[i] = i; }
    int find(int u) { return u ^ fa[u] ? fa[u] = find(fa[u]) : u; }
    bool connected(int u, int v) { return find(u) == find(v); }
    void merge(int u, int v) {
        u = find(u), v = find(v);
        if (u != v) fa[u] = v;
    }
};

static edge g[Maxm];
static int n, m;
static int ans = INT_MAX, cntNode;
static int virtualId[Maxn + Maxm];
pair <int, int> lst[Maxm + Maxn];
DSU <Maxn + Maxm> ufs;
LCT <Maxn + Maxm> tur;

void init() {
    n = read(), m = read();
    rep (i, 1, m) {
        int u = read(), v = read(), a = read(), b = read();
        g[i] = (edge){u, v, a, b};
    }

    ufs.init();
    rep (i, 1, n) virtualId[i] = tur.newnode(0);
/**/cntNode = n;
}

inline bool Connected(int u, int v) { return ufs.connected(u, v); }
inline void Link(int u, int v, int val) {
    virtualId[++cntNode] = tur.newnode(val);
    tur.link(virtualId[u], virtualId[cntNode]); 
    tur.link(virtualId[v], virtualId[cntNode]);
    lst[cntNode] = make_pair(u, v);
    ufs.merge(u, v);
}
inline int Query(int u, int v) { return tur.split(virtualId[u], virtualId[v]).first; }
inline void Cut(int u, int v) {
    int res = tur.split(u, v).second;
    tur.cut(lst[res].first, res); tur.cut(lst[res].second, res);
}

void solve() {
    sort(g + 1, g + m + 1);

    rep (i, 1, m) {
        bool addFlag = false;
        int u = g[i].u, v = g[i].v, a = g[i].a, b = g[i].b;

        if (Connected(u, v) == false) {
            addFlag = true;
            Link(u, v, b);
        } else 
            if (b < Query(u, v)) Cut(u, v), Link(u, v, b), addFlag = true;

        if (addFlag && Connected(1, n) == true) ans = min(ans, a + Query(1, n));
    }

    cout << (ans == INT_MAX ? -1 : ans) << endl;
}

int main() {
//  freopen("LG2387.in", "r", stdin);
//  freopen("LG2387.out", "w", stdout);

    init();
    solve();

#ifdef Qrsikno
    debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
    return 0;
}

转载于:https://www.cnblogs.com/qrsikno/p/10246985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
P2375 [NOI2014] 动物园是一道经典的动态规划题目,以下是该题的详细题意和解题思路。 【题意描述】 有两个长度为 $n$ 的整数序列 $a$ 和 $b$,你需要从这两个序列中各选出一些数,使得这些数构成一个新的序列 $c$。其中,$c$ 序列中的元素必须在原序列中严格递增。每个元素都有一个价值,你的任务是选出的元素的总价值最大。 【解题思路】 这是一道经典的动态规划题目,可以采用记忆化搜索的方法解决,也可以采用递推的方法解决。 记忆化搜索的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int dfs(int x, int y) { if (dp[x][y] != -1) return dp[x][y]; if (x == n || y == n) return 0; int res = max(dfs(x + 1, y), dfs(x + 1, y + 1)); if (a[x] > b[y]) { res = max(res, dfs(x, y + 1) + b[y]); } return dp[x][y] = res; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); memset(dp, -1, sizeof(dp)); printf("%d\n", dfs(0, 0)); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值,-1表示未计算过。dfs(x,y)表示选到a数组中第x个元素和b数组中第y个元素时的最大价值,如果dp[x][y]已经计算过,则直接返回dp[x][y]的值。如果x==n或者y==n,表示已经遍历完一个数组,直接返回0。然后就是状态转移方程了,如果a[x] > b[y],则可以尝试选b[y],递归调用dfs(x, y+1)计算以后的最大价值。否则,只能继续遍历数组a,递归调用dfs(x+1, y)计算最大价值。最后,返回dp[0][0]的值即可。 递推的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]); if (a[i] > b[j]) { dp[i][j] = max(dp[i][j], dp[i][j + 1] + b[j]); } } } printf("%d\n", dp[0][0]); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值。从后往前遍历数组a和数组b,依次计算dp[i][j]的值。状态转移方程和记忆化搜索的方法是一样的。 【参考链接】 P2375 [NOI2014] 动物园:https://www.luogu.com.cn/problem/P2375

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值