3669: [Noi2014]魔法森林

两种方法,一种SPFA动态加边,一种lct维护最小生成树...

但是思路大致一样,考虑枚举其中一种值,比如a,然后用b的值做最小生成树,然后找到1到n之间的这条链的最大值即为答案

c++代码如下:

1.SPFA动态加边:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
    x = 0;char c;int sign = 1;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
    x *= sign;
}

const int N = 1e5+500,M = 2e5+500;
int n,m,ans,d[N];
int head[N],to[M],nxt[M],w[M],tot;
struct Str { int u,v,a,b; }e[M];
bool vis[N];

const bool cmp(Str a,Str b) { return a.a < b.a || a.a == b.a && a.b < b.b; }

inline void add(int x,int y,int val)
{
    w[tot] = val;
    to[tot] = y;
    nxt[tot] = head[x];
    head[x] = tot ++;
}

int main()
{
    memset(head,-1,sizeof head); ans = 1e9+7;
    memset(d,0x3f,sizeof d); d[1] = 0;
    read(n); read(m);
    rep(i,1,m)
    {
        read(e[i].u); read(e[i].v);
        read(e[i].a); read(e[i].b);
    }
    
    sort(e + 1,e + m + 1,cmp);
    
    queue<int>q;
    d[1] = 0;
    rep(i,1,m)
    {
        add(e[i].u,e[i].v,e[i].b);
        add(e[i].v,e[i].u,e[i].b);
        q.push(1);q.push(e[i].u);q.push(e[i].v);
        vis[e[i].u] = vis[e[i].v] = vis[1] = 1;
        while(!q.empty())
        {
            int x = q.front();q.pop();vis[x] = 0;
            for(register int i = head[x];~i;i = nxt[i])
                if(d[to[i]] > max(d[x],w[i]))
                {
                    d[to[i]] = max(d[x],w[i]);
                    if(!vis[to[i]])
                     	q.push(to[i]),vis[to[i]] = 1;
                }
        }
        ans = min(ans,e[i].a + d[n]);
    }
    
    cout << (ans == 1e9+7?-1:ans) << endl;
    
    return 0;
}

2:lct 维护最小生成树:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
    x = 0;char c;int sign = 1;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
    x *= sign;
}

const int N = 1e5+500,M = 2e5+500;
int n,m,ans,p[N];
struct Str { int u,v,a,b; }e[M];
struct Link_Cut_Tree
{
    int ch[N+M][2],f[N+M],num[N+M],val[N+M],rev[N+M];
    
    #define lc ch[x][0]
    #define rc ch[x][1]
    
    inline bool get(int x) { return x == ch[f[x]][1]; }
    
    inline bool isroot(int x) { return !(ch[f[x]][1] == x) && !(ch[f[x]][0] == x) ; }
    
    inline void update(int x)
    {
        val[x] = x;
        if(num[val[x]] < num[val[lc]])
            val[x] = val[lc];
        if(num[val[x]] < num[val[rc]])
            val[x] = val[rc];
    }
    
    inline void rotate(int x)
    {
        int fa = f[x],t = get(x);
        
        f[x] = f[fa];
        if(!isroot(fa)) ch[f[x]][get(fa)] = x;
        f[fa] = x;
        ch[fa][t] = ch[x][t^1];
        if(ch[x][t^1]) f[ch[x][t^1]] = fa;
        ch[x][t^1] = fa;
        
        update(fa); update(x);
    }
    
    inline void put_down(int x)
    {
        if(!isroot(x) ) put_down(f[x]);
        if(rev[x])
        {
            rev[x] ^= 1;rev[lc] ^= 1;rev[rc] ^= 1;
            swap(lc,rc);
        }
    }
    
    inline void splay(int x)
    {
        put_down(x);
        
        while(!isroot(x))
        {
            if(!isroot(f[x])) rotate(get(f[x]) == get(x) ? f[x] : x);
            rotate(x);
        }
    }
    
    inline void access(int x)
    {
        int t = 0;
        while(x)
        {
            splay(x);
            ch[x][1] = t;
            update(x);
            t = x;
            x = f[x];
        }
    }
    
    inline void mkroot(int x) { access(x); splay(x); rev[x] ^= 1; }
    
    inline void Link(int x,int y) { mkroot(x); f[x] = y; }
    
    inline void cut(int x,int y)
    {
        mkroot(x); access(y); splay(y);
        f[x] = 0; ch[y][0] = 0;
        update(x); update(y);
    }
    
    int query(int x,int y)
    {
        mkroot(x);
        access(y);
        splay(y);
        return val[y];
    }
    
}lct;

int get_fa(int x) { return p[x] == x ? x : p[x] = get_fa(p[x]); }

const bool cmp(Str a,Str b) { return a.a < b.a; }

int main()
{
    ans = 1e9+7; 
    read(n); read(m);
    rep(i,1,m)
    {
        read(e[i].u); read(e[i].v);
        read(e[i].a); read(e[i].b);
    }
    
    sort(e + 1,e + 1 + m,cmp);
    
    rep(i,1,n) p[i] = i;
    
    rep(i,1,m) lct.num[i + n] = e[i].b;
    
    rep(i,1,m)
    {
        int x = get_fa(e[i].u),y = get_fa(e[i].v);
        if(x != y)
        {
            p[x] = y;
            lct.Link(e[i].u,i + n);
            lct.Link(e[i].v,i + n);
        }else 
        {
            int cnt = lct.query(e[i].u,e[i].v);
            if(e[i].b < lct.num[cnt])
            {
                lct.cut(e[cnt - n].u,cnt); lct.cut(e[cnt - n].v,cnt);
                lct.Link(e[i].u,i + n); lct.Link(e[i].v,i + n);
            }
        }
        
        if(get_fa(1) == get_fa(n)) ans = min(ans,e[i].a + lct.num[lct.query(1,n)]);
    }
    
    cout << (ans == 1e9+7 ? -1 : ans) << endl;
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值