• codeforces CF786B CF787D Legacy 线段树优化建图


    $ Rightarrow $ 戳我进CF原题

    D. Legacy


    time limit per test: 2 seconds
    memory limit per test: 256 megabytes
    input: standard input
    output: standard output

     

    Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them.
    So Rick wants to give his legacy to Morty before bad guys catch them.
     
    There are $ n $ planets in their universe numbered from $ 1 $ to $ n $ .
    Rick is in planet number $ s $ (the earth) and he doesn't know where Morty is.
    As we all know, Rick owns a portal gun.
    With this gun he can open one-way portal from a planet he is in to any other planet (including that planet).
    But there are limits on this gun because he's still using its free trial.

    By default he can not open any portal by this gun.
    There are $ q $ plans in the website that sells these guns.
    Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
     
    Plans on the website have three types:
     
    1.With a plan of this type you can open a portal from planet $ v $ to planet $ u $.
    2.With a plan of this type you can open a portal from planet $ v $ to any planet with index in range $ [l, r] $.
    3.With a plan of this type you can open a portal from any planet with index in range $ [l, r] $ to planet $ v $.
     
    Rick doesn't known where Morty is,
    but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately.
    So for each planet (including earth itself) he wants to know
    the minimum amount of money he needs to get from earth to that planet.
     

    Input

    The first line of input contains three integers $ n, q $ and $ s (1 ≤ n, q ≤ 10^5, 1 ≤ s ≤ n) $
    — number of planets, number of plans and index of earth respectively.
     
    The next $ q $ lines contain the plans.
    Each line starts with a number $ t $ , type of that plan $ (1 ≤ t ≤ 3) $ .
    If $ t = 1 $ then it is followed by three integers $ v, u $ and $ w $ where $ w $ is the cost of that plan $ (1 ≤ v, u ≤ n, 1 ≤ w ≤ 10^9) $ .
    Otherwise it is followed by four integers $ v, l, r $ and $ w $ where $ w $ is the cost of that plan $ (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 10^9) $ .
     

    Output

    In the first and only line of output print $ n $ integers separated by spaces.
    $ i $-th of them should be minimum money to get from earth to $ i $-th planet, or $  -1 $ if it's impossible to get to that planet.
     

    Examples

    input1

     3 5 1
     2 3 2 3 17
     2 3 2 2 16
     2 2 2 3 3
     3 3 1 1 12
     1 3 3 17
    

    output1

     0 28 12
    

    input2

     4 3 1
     3 4 1 3 12
     2 2 3 4 10
     1 2 4 16
    

    output2

     0 -1 -1 12
    

     

    Note

    In the first sample testcase, Rick can purchase $ 4 $ th plan once
    and then $ 2 $ nd plan in order to get to get to planet number $ 2 $ .
     

    题目大意

    • $ n(n le 10^5) $ 个点构成的有向图,有 $ m(m le 10^5) $ 条连通信息,信息有三种:

    • $ 1 quad u quad v quad w $ ,表示存在一条边权为 $ w $ 的有向边 $ (u,v)) $ ;

    • $ 2 quad u quad L quad R quad w $ ,表示 $ forall v in quad [L,R] $ ,存在一条边权为 $ w $ 的有向边 $ (u,v) $ ;

    • $ 3 quad u quad L quad R quad w $ ,表示 $ forall v in quad [L,R] $ ,存在一条边权为 $ w $ 的有向边 $ (u,v) $ 。

    • 其中 $ w le 10^9 $ 。求点 $ s $ 到每个点的最短路,不存在输出 $ -1 $。
       

    思路

    • 线段树优化建图。

    • 建立两棵线段树,其上点的点权分别表示“到达这个区间内所有点的最小花费”和“到达这个区间内任意一个点的最小花费”。

    • 对于第一种路直接加边即可

    • 对于第二种路,添加从 $ v $ 到第一棵线段树对应区间中的点的边

    • 对于第三种路,添加从从第二棵线段树对应区间中的点到 $ v $ 的边
       

    代码

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #define N 100005
    #define M 300005
    #define ll long long
    using namespace std;
    int n,m,s,cnt,rt1,rt2;
    int head[M],ls[M],rs[M],tot;
    struct edge{ int v,w,nxt; }e[N * 20];
    inline void add(int u,int v,int w) { 
        e[++tot].v=v; e[tot].w=w; e[tot].nxt=head[u]; head[u]=tot;
    }
    void build1(int &o,int l,int r) { 
        if(l==r){ o=l; return; }
        o=++cnt; 
        int mid=l+r>>1;
        build1(ls[o],l,mid); build1(rs[o],mid+1,r);
        add(o,ls[o],0);      add(o,rs[o], 0); 
    }
    void build2(int &o,int l,int r) { 
        if(l==r){ o=l; return; }
        o=++cnt; 
        int mid=l+r>>1;
        build2(ls[o],l,mid); build2(rs[o],mid+1,r);
        add(ls[o],o,0);      add(rs[o],o, 0); 
    }
    int L,R;
    void updata(int o,int l,int r,int u,int w,int p) {
        if(L<=l&&r<=R) { 
            p==2 ? add(u, o, w) : add(o, u, w);
            return;
        }
        int mid=l+r>>1;
        if(L<=mid) updata(ls[o],l,mid,u,w,p);
        if(mid<R) updata(rs[o],mid+1,r,u,w,p);
    }
    const ll INF = 0x3F3F3F3F3F3F3F3F;
    ll dis[M];
    queue<int> Q;
    bool vis[M];
    void SPFA(int s) {  
        memset(dis,0x3F,sizeof(dis));
        dis[s] = 0; Q.push(s); vis[s]=1;
        while(!Q.empty()) {
            int u=Q.front(); Q.pop(); vis[u]=0;
            for(int i=head[u];i;i=e[i].nxt)
                if(dis[u]+e[i].w<dis[e[i].v]){
                    dis[e[i].v]=dis[u]+e[i].w;
                    if(!vis[e[i].v]){ vis[e[i].v]=1; Q.push(e[i].v); }
                }
        }
    }
    int main() {
        scanf("%d %d %d",&n,&m,&s);
        cnt = n;
        build1(rt1, 1, n); build2(rt2, 1, n);
        while(m--){
            int opt,u,v,w;
            scanf("%d",&opt);
            if(opt==1){
                scanf("%d %d %d",&u,&v,&w);
                add(u,v,w); 
            } else {
                scanf("%d %d %d %d",&u,&L,&R,&w);
                updata(opt == 2 ? rt1 : rt2,1,n,u,w,opt);
            }
        }
        SPFA(s);
        for(int i = 1; i <= n; i++) 
            printf("%lld ",dis[i]<INF ? dis[i] : -1);
        return 0;
    }
    
  • 相关阅读:
    web前端要学哪些?
    angularjs factory,service,provider 自定义服务的不同
    PSP软件开发过程
    Eclipse连接sqlserver体验过程
    在线制作GIF图片项目愿景与范围
    C# Hashtable vs Dictionary 学习笔记
    Unity 工作经历+近期面试经历
    配置文件加载的一个坑
    rabbitmq exchange type
    centos7下nginx添加到自定义系统服务中提示Access denied
  • 原文地址:https://www.cnblogs.com/PotremZ/p/CF786B.html
Copyright © 2020-2023  润新知