zoukankan      html  css  js  c++  java
  • [bzoj3669] [NOI2014]魔法森林

    Description

    为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士。魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M。初始时小E同学在号节点1,隐士则住在号节点N。小E需要通过这一片魔法森林,才能够拜访到隐士。

    魔法森林中居住了一些妖怪。每当有人经过一条边的时候,这条边上的妖怪就会对其发起攻击。幸运的是,在号节点住着两种守护精灵:A型守护精灵与B型守护精灵。小E可以借助它们的力量,达到自己的目的。

    只要小E带上足够多的守护精灵,妖怪们就不会发起攻击了。具体来说,无向图中的每一条边Ei包含两个权值Ai与Bi。若身上携带的A型守护精灵个数不少于Ai,且B型守护精灵个数不少于Bi,这条边上的妖怪就不会对通过这条边的人发起攻击。当且仅当通过这片魔法森林的过程中没有任意一条边的妖怪向小E发起攻击,他才能成功找到隐士。

    由于携带守护精灵是一件非常麻烦的事,小E想要知道,要能够成功拜访到隐士,最少需要携带守护精灵的总个数。守护精灵的总个数为A型守护精灵的个数与B型守护精灵的个数之和。

    Input

    第1行包含两个整数N,M,表示无向图共有N个节点,M条边。 接下来M行,第行包含4个正整数Xi,Yi,Ai,Bi,描述第i条无向边。其中Xi与Yi为该边两个端点的标号,Ai与Bi的含义如题所述。 注意数据中可能包含重边与自环。

    Output

    输出一行一个整数:如果小E可以成功拜访到隐士,输出小E最少需要携带的守护精灵的总个数;如果无论如何小E都无法拜访到隐士,输出“-1”(不含引号)。

    HINT

    2<=n<=50,000

    0<=m<=100,000

    1<=ai ,bi<=50,000

    solution

    题目让求的是(max{a_i}+max{b_i}),这个并不好维护,所以可以考虑枚举(a_i),然后考虑(b_i)的影响。

    做法就先把边按(a_i)排序一遍,然后一个一个加边,维护一颗树,如果加成环了就把链上最大的删掉。

    然后连边删边(LCT)维护下就行了。

    #include<bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    void read(int &x) {
        x=0;int f=1;char ch=getchar();
        for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
        for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
    }
    
    void print(int x) {
        if(x<0) putchar('-'),x=-x;
        if(!x) return ;print(x/10),putchar(x%10+48);
    }
    void write(int x) {if(!x) putchar('0');else print(x);putchar('
    ');}
    
    const int maxn = 3e5+1;
    
    int A[maxn],B[maxn],tot,n,m;
    
    struct Link_Cut_Tree {
        int fa[maxn],son[maxn][2],rev[maxn],val[maxn],pos[maxn],sta[maxn];
        void update(int x) {
            pos[x]=x;
            if(val[pos[son[x][0]]]>val[pos[x]]) pos[x]=pos[son[x][0]];
            if(val[pos[son[x][1]]]>val[pos[x]]) pos[x]=pos[son[x][1]];
        }
        void push_rev(int x) {rev[x]^=1,swap(son[x][0],son[x][1]);}
        void pushdown(int x) {
            if(!rev[x]) return ;
            rev[x]^=1,push_rev(son[x][0]),push_rev(son[x][1]);
        }
        int is_root(int x) {return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;}
        int which(int x) {return son[fa[x]][1]==x;}
        void rotate(int x) {
            int f=fa[x],ff=fa[f],w=which(x);
            if(!is_root(f)) son[ff][son[ff][1]==f]=x;
            fa[f]=x,fa[x]=ff;son[f][w]=son[x][w^1],fa[son[x][w^1]]=f,son[x][w^1]=f;
            update(f),update(x);
        }
        void splay(int x) {
    		int t=x,top=0;
    		while(!is_root(t)) sta[++top]=t,t=fa[t];
    		sta[++top]=t;
    		while(top) pushdown(sta[top--]);
            while(!is_root(x)) {
                int y=fa[x],z=fa[y];
                if(!is_root(y)) rotate((son[y][1]==x)^(son[z][1]==y)?x:y);
                rotate(x);
            }update(x);
        }
        void access(int x) {
            for(int t=0;x;t=x,x=fa[x]) splay(x),son[x][1]=t,update(x);
        }
        void make_root(int x) {
            access(x),splay(x),push_rev(x);
        }
        void split(int x,int y) {
            make_root(x),access(y),splay(y);
        }
        void link(int x,int y) {
            make_root(x),fa[x]=y;
        }
        void cut(int x,int y) {
            make_root(x),access(y),splay(y);
            fa[x]=son[y][0]=0;update(y);
        }
        int find_root(int x) {
            access(x),splay(x);
            while(son[x][0]) x=son[x][0];return x;
        }
        int query(int x,int y) {
            if(find_root(x)!=find_root(y)) return 1e18;
            split(x,y);return val[pos[y]];
        }
        void Link(int x,int y,int z) {
            if(find_root(x)!=find_root(y)) {
                val[++tot]=z,link(tot,A[tot]=x),link(tot,B[tot]=y);
                return ;
            }
            split(x,y);int t=pos[y];
            if(val[t]<=z) return ;
            cut(t,A[t]),cut(t,B[t]);
            val[t]=z,link(t,A[t]=x),link(t,B[t]=y);
        }
        
    }LCT;
    
    struct edge{
        int fr,to,a,b;
        bool operator < (const edge &rhs) const {return a<rhs.a;}
    }e[maxn];
    
    signed main() {
        read(n),read(m);tot=n;
        for(int i=1;i<=m;i++) read(e[i].fr),read(e[i].to),read(e[i].a),read(e[i].b);
        sort(e+1,e+m+1);int ans=1e18;
        for(int i=1;i<=m;i++) 
            LCT.Link(e[i].fr,e[i].to,e[i].b),ans=min(ans,e[i].a+LCT.query(1,n));
        if(ans==1e18) ans=-1;
        write(ans);
        return 0;
    }
    
  • 相关阅读:
    德才真值表
    Linaro公司基于GCC推出的的ARM交叉编译工具
    荣耀4CROOT 成功!附本人ROOT过程——KINGROOT
    batman-adv——B.A.T.M.A.N. Advanced quick start guide
    linux内核外部驱动模块编译报错ERROR—drivers/*.ko] undefined
    Linux Kernel and Driver Development Training——linux-kernel-slides
    华为手机——解锁步骤
    编译Linux内核—浅谈EABI和OABI
    Linux Kernel and Driver Development Training
    Android—Step by step
  • 原文地址:https://www.cnblogs.com/hbyer/p/10138126.html
Copyright © 2011-2022 走看看