[NOI2014]魔法森林【LCT】

题目链接


  有N个点,M条边,要使得从1点到N点的(最低要求ai和最低要求bi)的和最小,问最小和。

  那么,很显然的,就是求一个联通关系,与最短路无关,因为限制条件不唯一,需要同时限制ai和bi,所以我们不妨枚举一维,然后再是维护一维。

  我们对A关键字进行升序处理,然后我们维护一棵B关键字的最小生成树,然后枚举这样的最小生成树的答案不就可以了吗?

  我们不断的进行加边操作,然后对B关键字操作,每次看新的B能否替换之前的B树,这就是我们维护的最小生成树了,然后更新答案即可。

  更新答案通过查询现在的枚举到的A关键字的值,再加上此时1~N路径上的最大的B关键字。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 15e4 + 7;
int N, M;
namespace LCT
{
    int fa[maxN], c[maxN][2];
    int r[maxN];
    pair<int, int> v[maxN], s[maxN];
    bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
    void pushup(int x)
    {
        s[x] = max(max(s[c[x][0]], s[c[x][1]]), v[x]);
    }
    void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
    void pushdown(int x)
    {
        if(r[x])
        {
            if(c[x][0]) pushr(c[x][0]);
            if(c[x][1]) pushr(c[x][1]);
            r[x] = 0;
        }
    }
    void Rotate(int x)
    {
        int y = fa[x], z = fa[y], k = c[y][1] == x;
        if(!isroot(y)) c[z][c[z][1] == y] = x;
        fa[x] = z;
        c[y][k] = c[x][k ^ 1];
        fa[c[x][k ^ 1]] = y;
        c[x][k ^ 1] = y;
        fa[y] = x;
        pushup(y);
        pushup(x);
    }
    int Stap[maxN];
    void Splay(int x)
    {
        int y = x, z = 0;
        Stap[++z] = y;
        while(!isroot(y)) Stap[++z] = y = fa[y];
        while(z) pushdown(Stap[z--]);
        while(!isroot(x))
        {
            y = fa[x]; z = fa[y];
            if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
            Rotate(x);
        }
    }
    void Access(int x)
    {
        int y = 0;
        while(x)
        {
            Splay(x);
            c[x][1] = y;
            pushup(x);
            y = x;
            x = fa[x];
        }
    }
    void makeroot(int x)
    {
        Access(x);
        Splay(x);
        pushr(x);
    }
    int findroot(int x)
    {
        Access(x);
        Splay(x);
        while(c[x][0])
        {
            pushdown(x);
            x = c[x][0];
        }
        Splay(x);
        return x;
    }
    void Split(int x, int y)
    {
        makeroot(x);
        Access(y);
        Splay(y);
    }
    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            fa[x] = y;
        }
    }
    void cut(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x || fa[y] != x || c[y][0]) return;
        fa[y] = c[x][1] = 0;
        pushup(x);
    }
};
using namespace LCT;
struct Edge
{
    int u, v, a, b;
    inline void In() { scanf("%d%d%d%d", &u, &v, &a, &b); }
    friend bool operator < (Edge e1, Edge e2) { return e1.a == e2.a ? e1.b < e2.b : e1.a < e2.a; }
} E[maxN];
int root[maxN];
int fid(int x) { return x == root[x] ? x : root[x] = fid(root[x]); }
int main()
{
    scanf("%d%d", &N, &M);
    for(int i=1; i<=N; i++) root[i] = i;
    for(int i=1; i<=M; i++) E[i].In();
    sort(E + 1, E + M + 1);
    for(int i=1; i<=M; i++) v[N + i] = make_pair(E[i].b, i);
    int ans = INF;
    v[0] = make_pair(0, 0);
    int id, du, dv, old_A = 0;
    for(int i=1, u, v, fu, fv; i<=M; i++)
    {
        u = E[i].u; v = E[i].v;
        fu = fid(u); fv = fid(v);
        if(fu ^ fv)
        {
            root[fu] = fv;
            link(u, N + i);
            link(v, N + i);
            old_A = E[i].a;
        }
        else
        {
            Split(u, v);
            if(s[v].first > E[i].b)
            {
                id = s[v].second;
                du = E[id].u;
                dv = E[id].v;
                cut(du, N + id);
                cut(dv, N + id);
                link(u, N + i);
                link(v, N + i);
                old_A = E[i].a;
            }
        }
        if(fid(1) == fid(N))
        {
            Split(1, N);
            ans = min(ans, old_A + s[N].first);
        }
    }
    if(ans == INF) printf("-1\n");
    else printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/107861393