UOJ #3(【NOI2014】魔法森林-LCT区间最值)

统计

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

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

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

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

输入格式

第1行包含两个整数 n,m ,表示无向图共有 n 个节点,m 条边。

接下来 m 行,第 i+1 行包含4个正整数 xi,yi,ai,bi ,描述第 i 条无向边。其中 xi yi 为该边两个端点的标号, ai bi 的含义如题所述。

注意数据中可能包含重边与自环。

输出格式

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

样例一

input
4 5
1 2 19 1
2 3 8 12
2 4 12 15
1 3 17 8
3 4 1 17

output
32

explanation

如果小E走路径1→2→4,需要携带 19+15=34 个守护精灵;

如果小E走路径1→3→4,需要携带 17+17=34 个守护精灵;

如果小E走路径1→2→3→4,需要携带 19+17=36 个守护精灵;

如果小E走路径1→3→2→4,需要携带 17+15=32 个守护精灵。

综上所述,小E最少需要携带 32 个守护精灵。

样例二

input
3 1
1 2 1 1

output
-1

explanation

小E无法从1号节点到达3号节点,故输出-1。

样例三

见样例数据下载

限制与约定

测试点编号 n m ai,bi
1 2n5 0m10 1ai,bi10
2
3
4 2n500 0m3000 1ai,bi50000
5
6
7 2n5000 0m10000
8
9
10
11 2n50000 0m100000 1ai30
1bi50000
12
13
14
15 1ai,bi50000
16
17
18
19
20

时间限制 3s

空间限制 512MB

把边用ai排序
直接用LCT去维护图
如果形成环,就删除这个环上的bi最大边

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (250000+10)
#define MAXM (200000+10)
#define mp make_pair  
#define fi first
#define se second
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

int n,m;

class LCT
{
public:
    int father[MAXN],siz[MAXN];
    int ch[MAXN][2];
    bool root[MAXN];
    bool rev[MAXN];
    ll val[MAXN];
    pair<ll,int> maxv[MAXN];
    void mem(int n)
    {
        MEM(father) MEM(siz) MEM(root)
        For(i,n+1) siz[i]=root[i]=1,maxv[i]=mp(0,i);    root[0]=1;
        MEM(ch)  MEM(rev)
        MEM(val)
    }

    void set(int x,ll bi){
        val[x]=bi;
        maxv[x]=mp(bi,x); 
    }

    void pushdown(int x)
    {
        if (!x) return ; 
        if (rev[x]) {
            if (ch[x][0]) rev[ch[x][0]]^=1;
            if (ch[x][1]) rev[ch[x][1]]^=1;
            swap(ch[x][0],ch[x][1]);
            rev[x]^=1;
        } 
    }
    void maintain(int x)
    {
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
        maxv[x]=max(max(maxv[ch[x][0]],maxv[ch[x][1]]),mp(val[x],x)); 

    }
    void rotate(int x)
    {
        int y=father[x],kind=ch[y][1]==x;

        ch[y][kind]=ch[x][!kind];
        if (ch[y][kind]) {
            father[ch[y][kind]]=y;
        }
        father[x]=father[y];
        father[y]=x;
        ch[x][!kind]=y;
        if (root[y])
        {
            root[x]=1;root[y]=0;
        }
        else
        {
            ch[father[x]][ ch[father[x]][1]==y ] = x;
        }
        maintain(y);maintain(x);
    }

    void P(int x)
    {
        if (!root[x]) P(father[x]);
        pushdown(x);
    }

    void splay(int x)
    {
        P(x);
        while(!root[x])
        {
            int y=father[x];
            int z=father[y];
            if (root[y]) rotate(x);
            else if ( (ch[y][1]==x)^(ch[z][1]==y) )
            {
                rotate(x); rotate(x);
            } 
            else 
            {
                rotate(y); rotate(x);
            }
        } 
    }



    int access(int x)
    {
        int y=0;
        do
        {
            splay(x);
            if (ch[x][1]) root[ch[x][1]]=1;
            ch[x][1]=y;
            if (y) root[y]=0;       
            maintain(x);
            y = x;
            x=father [x]; 
        } while (x) ;
        return y;
    }

    void cut(int x)
    {
        access(x);
        splay(x);

        father[ch[x][0]]=0;
        root[ch[x][0]]=1;
        ch[x][0]=0;
        maintain(x);
    }

    void join(int x,int y)
    {
        make_root(x);
        access(y);
        splay(y);
        ch[y][1]=x;
        father[x]=y;
        maintain(y);
        root[x]=0;
    }
    void reverse(int x){
        rev[x]^=1;   // 标记记完后迅速处理 
    }
    void make_root(int x){
        access(x);splay(x);
        reverse(x);pushdown(x);
    }
    int get_root(int x){
        access(x);
        splay(x);
        do {
            pushdown(x);
            if (ch[x][0]) x=ch[x][0];
            else break;
        }while(1);
        return x;
    }

    bool find(int x,int y)
    {
        while (father[x]) x=father[x];
        while (father[y]) y=father[y];
        return x==y; 
    }

    pair<ll,int> query(int x,int y) {
        make_root(x);
        access(y);
        splay(y);
        return maxv[y];
    }

}S;

struct Edge{
    int x,y,a,b;
    friend bool operator<(Edge a,Edge b) {
        return a.a!=b.a? a.a<b.a : a.b<b.b; 
    }
    void scan(){
        scanf("%d%d%d%d",&x,&y,&a,&b);
    }
}e[MAXM];


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

    scanf("%d%d",&n,&m);
    S.mem(n+m);
    For(i,m) {
        e[i].scan();     
    }
    sort(e+1,e+1+m);
    For(i,m) S.set(n+i,e[i].b);



    ll ans=INF;
    For(i,m) {
        if (e[i].x==e[i].y) continue;
        if (S.find(e[i].x,e[i].y)) {
            pair<ll,int> p= S.query(e[i].x,e[i].y);
            if (p.fi<=e[i].b) continue;
            int u=e[p.se-n].x,v=e[p.se-n].y; 
            S. make_root(p.se);
            S.cut(u); S.cut(v);
        } 

        S.join(e[i].x,n+i); 
        S.join(e[i].y,n+i); 

        if (!S.find(1,n)) continue; 
        ll nowans=e[i].a+S.query(1,n).first;

        ans=min(ans,nowans);

    }

    if (ans==INF) puts("-1");
    else printf("%d\n",(int)ans); 



    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值