【NOI2014】【BZOJ3669】魔法森林

本文介绍了一种利用链剖树(LCT)解决特定无向图最小花费路径问题的方法。通过对边权进行预处理并使用LCT维护最小生成树,算法能够有效地找到满足条件的最小花费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目链接】

【前置技能】

  • LCT

【题解】

  • 题意是给出一张无向图,边权为 a i , b i a_i,b_i ai,bi,定义花费为经过的边的 m a x { a i } + m a x { b i } max\{a_i\} + max\{b_i\} max{ai}+max{bi},最小化花费。
  • 如果只有一个边权,直接走最小生成树即可。考虑按 a i a_i ai从小到大向树上加边,即枚举 m a x { a i } max\{a_i\} max{ai},然后用LCT维护 b i b_i bi的最小生成树。答案就是当前枚举的 a i a_i ai加上 1 1 1~ n n n路径上的 b i b_i bi的最大值。
  • 发现可能并不会走到枚举的 a i a_i ai最大的边,但是这种情况一定比最优解更劣,所以对于答案没有影响。
  • 具体实现的时候可以对每个图上的点和边都在LCT中建一个点,若点 u u u v v v通过边 x x x连接起来,就在LCT中 l i n k ( u , x ) link(u, x) link(u,x) l i n k ( v , x ) link(v, x) link(v,x)
  • 时间复杂度 O ( M l o g ( N + M ) ) O(Mlog(N + M)) O(Mlog(N+M))

【代码】

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXM    100010
#define MAXN    50010
using namespace std;
int n, m, ans;
struct edg{int u, v, a, b;}e[MAXM];
 
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
 
bool cmpa(edg a, edg b){
    return a.a < b.a;
}
 
struct Link_Cut_Tree{
    struct info{int son[2], fa, up, tag, maxn, ind, id;}a[MAXM + MAXN];
    int cnt;
    void create(int k){
        ++cnt;
        a[cnt].ind = a[cnt].maxn = k;
        a[cnt].id = cnt;
    }
    int get(int pos){
        return (pos == a[a[pos].fa].son[1]);
    }
    void push_down(int pos){
        if (!a[pos].tag) return;
        a[pos].tag = 0;
        swap(a[pos].son[0], a[pos].son[1]);
        if (a[pos].son[0]) a[a[pos].son[0]].tag ^= 1;
        if (a[pos].son[1]) a[a[pos].son[1]].tag ^= 1;
    }
    void push_up(int pos){
        a[pos].maxn = a[pos].ind, a[pos].id = pos;
        if (a[pos].son[0] && a[pos].maxn < a[a[pos].son[0]].maxn)
            a[pos].maxn = a[a[pos].son[0]].maxn, a[pos].id = a[a[pos].son[0]].id;
        if (a[pos].son[1] && a[pos].maxn < a[a[pos].son[1]].maxn)
            a[pos].maxn = a[a[pos].son[1]].maxn, a[pos].id = a[a[pos].son[1]].id;
    }
    void rotate(int pos){
        int fa = a[pos].fa, gr = a[fa].fa;
        push_down(fa), push_down(pos);
        a[pos].up = a[fa].up, a[fa].up = 0;
        int x = get(pos), f = get(fa);
        if (a[pos].son[x ^ 1]) a[a[pos].son[x ^ 1]].fa = fa;
        a[fa].son[x] = a[pos].son[x ^ 1];
        a[fa].fa = pos;
        a[pos].son[x ^ 1] = fa;
        if (gr) a[gr].son[f] = pos;
        a[pos].fa = gr;
        push_up(fa), push_up(pos);
    }
    void splay(int pos){
        push_down(pos);
        for (int fa = a[pos].fa; (fa = a[pos].fa); rotate(pos))
            if (a[fa].fa){
                if (get(pos) == get(fa)) rotate(fa);
                else rotate(pos);
            }
    }
    void access(int x){
        splay(x);
        int tmp = a[x].son[1];
        if (tmp) a[tmp].fa = 0, a[tmp].up = x;
        a[x].son[1] = 0;
        push_up(x);
        while (a[x].up){
            int f = a[x].up;
            splay(f);
            int tmp = a[f].son[1];
            if (tmp) a[tmp].fa = 0, a[tmp].up = f;
            a[f].son[1] = x, a[x].fa = f, a[x].up = 0;
            push_up(f);
            splay(x);
        }
    }
    int findroot(int x){
        access(x);
        push_down(x);
        while (a[x].son[0]){
            x = a[x].son[0];
            push_down(x);
        }
        splay(x);
        return x;
    }
    void reverse(int x){
        access(x);
        a[x].tag ^= 1;
        push_down(x);
    }
    void link(int x, int y){
        access(x), reverse(y);
        a[x].son[1] = y, a[y].fa = x;
        push_up(x);
    }
    void cut(int x, int y){
        reverse(x), access(y), splay(x);
        a[x].son[1] = a[y].fa = 0;
        push_up(x);
    }
    void getans(int tnp){
        if (findroot(1) == findroot(n)) {
            reverse(1), access(n);
            int tmp = a[n].maxn;
            chkmin(ans, tmp + tnp);
        }
    }
}lct;
 
int main(){
    read(n), read(m);
    for (int i = 1; i <= m; ++i)
        read(e[i].u), read(e[i].v), read(e[i].a), read(e[i].b);
    sort(e + 1, e + 1 + m, cmpa);
    for (int i = 1; i <= n; ++i)
        lct.create(0);
    for (int i = 1; i <= m; ++i)
        lct.create(e[i].b);
    ans = INF;
    for (int i = 1; i <= m; ++i){
        int u = e[i].u, v = e[i].v, a = e[i].a, b = e[i].b;
        if (lct.findroot(u) == lct.findroot(v)) {
            int j = 1;
            lct.reverse(u), lct.access(v);
            if (b >= lct.a[v].maxn) continue;
            j = lct.a[v].id - n;
            lct.cut(e[j].u, j + n);
            lct.cut(e[j].v, j + n);
            lct.link(u, i + n);
            lct.link(v, i + n);
        } else {
            lct.link(u, i + n);
            lct.link(v, i + n);
        }
        lct.getans(a);
    }
    if (ans == INF) printf("-1\n");
    else printf("%d\n", ans); 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值