• 点上 最短路


    Problem Description
    There are n soda living in a straight line. soda are numbered by 1,2,,n from left to right. The distance between two adjacent soda is 1 meter. Every soda has a teleporter. The teleporter of i-th soda can teleport to the soda whose distance between i-th soda is no less than li and no larger than ri. The cost to use i-th soda's teleporter is ci.

    The 1-st soda is their leader and he wants to know the minimum cost needed to reach i-th soda (1in). 
     
    Input
    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contains an integer (1n2×105), the number of soda. 
    The second line contains n integers l1,l2,,ln. The third line contains n integers r1,r2,,rn. The fourth line contains n integers c1,c2,,cn. (0lirin,1ci109)
     
    Output
    For each case, output n integers where i-th integer denotes the minimum cost needed to reach i-th soda. If 1-st soda cannot reach i-the soda, you should just output -1.
     
    Sample Input
    1 5 2 0 0 0 1 3 1 1 0 5 1 1 1 1 1
     
    Sample Output
    0 2 1 1 -1
     
    题意 : 给你一些个点,告诉你这些个点可以到达哪些位置,从每个点出发都会有一个对应位置上的权值,算长度的时候得加上权值
    思路分析 : 首先这个题不能开数组去建边,边很多,会直接超内存,然后就是要对每个点能到的位置建一条虚边,正常的 dij 就能解决,但是如果是这样还是会超时,因为 dijkstra 虽然可以保证访问过并确定是最短路的点不会再去更新,但是还是会去在访问的,当每个点都可以到达所有点时,访问的复杂度也是很高的,会超时,那么我们这里可以用并查集去优化,当一个点更新后,将其与它相邻的点并起来
    注意 : 这个题目是区别于边上最短距离的,边上的最短距离,直接将距离最近点放入优先队列,但是点上的最小的距离,需要将到达此点的最小距离在加上此点到达其他点的花费放入优先队列
    代码示例 :
    #define ll long long
    const int maxn = 2e5+10;
    const ll inf = 1LL<<62;
    
    int n;
    int l[maxn], r[maxn];
    ll c[maxn], dis[maxn];
    int f[maxn];
    
    struct node
    {
        int to;
        ll cost;
        
        node(int _to=0, ll _cost=0):to(_to), cost(_cost){}
        bool operator< (const node &v) const{
            return cost > v.cost;
        }  
    };
    priority_queue<node>que;
    
    int fid(int x){
        if (x != f[x]) f[x] = fid(f[x]);
        return f[x];
    }
    
    void unit(int i, int j){
        int x1 = fid(i), x2 = fid(j);
    
        if (x1 != x2) {
            f[x1] = x2;
        }
            
    }
    
    void dij() {
        while(!que.empty()) que.pop();
        
        for(int i = 1; i <= n+3; i++) dis[i] = inf;        
        dis[1] = 0;
        for(int i = 1; i <= n+3; i++) f[i] = i;
        
        que.push(node(1, c[1]));
        while(!que.empty()){
            node v = que.top();
            que.pop();
            
            int pos = v.to; 
            for(int i = -1; i <= 1; i += 2){
                int L = pos+i*l[pos];
                int R = pos+i*r[pos];
                
                if (L > R) swap(L, R);
                L = max(L, 1);
                R = min(R, n);
                for(int j = L; j <= R; j++){
                    j = fid(j);
                    if (j > R) break;
                    if (v.cost < dis[j]){
                        dis[j] = v.cost;
                        que.push(node(j, dis[j]+c[j]));
                    }
                    unit(j, j+1);
                }
            }
        }
    }
    
    int main() {
        int t;
        
        cin >> t;
        while(t--){
            scanf("%d", &n);
            for(int i = 1; i <= n; i++) scanf("%d", &l[i]);
            for(int i = 1; i <= n; i++) scanf("%d", &r[i]);
            for(int i = 1; i <= n; i++) scanf("%lld", &c[i]);
            
            dij();
            for(int i = 1; i <= n; i++){
                printf("%lld%c", dis[i]==inf?-1:dis[i], i==n?'
    ':' ');
            }
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    linux下Boost序列化问题解决
    树莓派搭建基于flask的web服务器-通过移动端控制LED
    unison+inotify
    员工为什么会离职 (转)
    编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)
    Cassandra 原理介绍
    使用Go语言编写区块链P2P网络(译)(转)
    缓存击穿举例
    Cassandra 原理介绍
    Go学习之路
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8495795.html
Copyright © 2020-2023  润新知