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

    Description

    给出一个 (n) 个点, (m) 条边的无向图,每条边都有权值 (a_i,b_i) ,求一条从点 (1) 到点 (n) 的路径,使得这条路径上边的 (a_i,b_i) 最大值之和最小。 (2 leq n leq 5 imes 10^4, 0 leq m leq 1 imes 10^5)

    Solution

    将所有边按照 (a_i) 升序排序,依次加入,用 LCT 维护 (b_i) 的最小生成树

    每次加边后,如果 (1)(n) 连通,就更新一次答案

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1000000;
    
    int n,m,val[N];
    
    namespace lct {
    	int top, q[N], ch[N][2], fa[N], rev[N];
    	int mx[N];
    	inline void pushup(int x){
    		int v=max(val[x], max( val[mx[ch[x][0]]],val[mx[ch[x][1]]] ));
    		if(v==val[x]) mx[x]=x;
    		else if(v==val[mx[ch[x][0]]]) mx[x]=mx[ch[x][0]];
    		else mx[x]=mx[ch[x][1]];
    	}
    	inline void pushdown(int x){
    		if(!rev[x]) return;
    		rev[ch[x][0]]^=1;
    		rev[ch[x][1]]^=1;
    		rev[x]^=1;
    		swap(ch[x][0],ch[x][1]);
    	}
    	inline bool isroot(int x){
    		return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
    	}
    	inline void rotate(int p){
    		int q=fa[p], y=fa[q], x=ch[fa[p]][1]==p;
    		ch[q][x]=ch[p][x^1]; fa[ch[q][x]]=q;
    		ch[p][x^1]=q; fa[q]=p; fa[p]=y;
    		if(y) if(ch[y][0]==q) ch[y][0]=p;
    		else  if(ch[y][1]==q) ch[y][1]=p;
    		pushup(q); pushup(p);
    	}
    	inline void splay(int x){
    		q[top=1]=x;
    		for(int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i];
    		for(int i=top;i;i--) pushdown(q[i]);
    		for(;!isroot(x);rotate(x))
    			if(!isroot(fa[x]))
    				rotate((ch[fa[x]][0]==x)==(ch[fa[fa[x]]][0]==fa[x])?fa[x]:x);
    	}
    	void access(int x){
    		for(int t=0;x;t=x,x=fa[x])
    			splay(x),ch[x][1]=t,pushup(x);
    	}
    	void makeroot(int x){
    		access(x);
    		splay(x);
    		rev[x]^=1;
    	}
    	int find(int x){
    		access(x);
    		splay(x);
    		while(ch[x][0]) x=ch[x][0];
    		return x;
    	}
    	bool islinked(int x,int y) {
            return find(x)==find(y);
    	}
    	void split(int x,int y){
    		makeroot(x);
    		access(y);
    		splay(y);
    	}
    	void cut(int x,int y){
    		split(x,y);
    		if(ch[y][0]==x)
    			ch[y][0]=0, fa[x]=0;
    	}
    	void link(int x,int y){
    		makeroot(x);
    		fa[x]=y;
    	}
    	int query(int x,int y) {
            split(x,y);
            return mx[y];
    	}
    }
    
    struct edge {
        int id,u,v,a,b;
        bool operator < (const edge &x) {
            return a < x.a;
        }
    } e[N];
    
    namespace mst {
        void add(edge ee) {
            val[ee.id+n]=ee.b;
            if(lct::islinked(ee.u,ee.v)) {
                int mx=lct::query(ee.u,ee.v);
                if(val[mx]>ee.b) {
                    lct::cut(mx,e[mx-n].u);
                    lct::cut(mx,e[mx-n].v);
                    lct::link(ee.id+n,ee.u);
                    lct::link(ee.id+n,ee.v);
                }
            }
            else {
                lct::link(ee.id+n,ee.u);
                lct::link(ee.id+n,ee.v);
            }
        }
        int getans() {
            return val[lct::query(1,n)];
        }
    }
    
    signed main() {
        ios::sync_with_stdio(false);
        cin>>n>>m;
        for(int i=1;i<=m;i++) {
            edge &ee = e[i];
            cin>>ee.u>>ee.v>>ee.a>>ee.b;
        }
        sort(e+1,e+m+1);
        for(int i=1;i<=m;i++) e[i].id=i;
        int ans=1e9;
        for(int i=1;i<=m;i++) {
            mst::add(e[i]);
            if(lct::islinked(1,n)) {
                ans=min(ans,mst::getans()+e[i].a);
            }
        }
        if(ans==1e9) cout<<-1;
        else cout<<ans;
    }
    
    
  • 相关阅读:
    VMware Workstation 16.0 key (仅支持 intel 架构)
    使用思科ASA对指定端口、IP进行抓包
    轮子的意义
    dubbo学习(三)泛化
    dubbo学习(二)链接
    dubbo学习(一)线程池
    mips交叉编译:SQLite3
    生成证书脚本
    ssh连接不上 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    gmssl ocsp 验证证书
  • 原文地址:https://www.cnblogs.com/mollnn/p/13171386.html
Copyright © 2011-2022 走看看