• Codeforces 787D. Legacy 线段树建模+最短路


    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 ≤ 105, 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 ≤ 109). 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 ≤ 109).

    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
    Input
    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
    Output
    0 28 12 
    Input
    4 3 1
    3 4 1 3 12
    2 2 3 4 10
    1 2 4 16
    Output
    0 -1 -1 12 
    Note

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

    题目连接:http://codeforces.com/contest/787/problem/D

    题意:有n个点,q个询问,每次询问有一种操作。操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w;操作2:[l,r]→u的距离为w;操作3:u到v的距离为w;求起点到其他点的最短距离,到达不了输出-1。

    思路:线段树建立模型,搜索最短路。最好不要用map之类的,容易超时。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int MAXN=5e6+100,inf=0x3f3f3f3f,MOD=1e9+7;
    const ll INF=1e17+10;
    struct node
    {
        int to;
        ll w;
        int next;
    } edge[MAXN];
    int cou,head[MAXN];
    int Max;
    int vis[MAXN];
    ll ans[MAXN];
    void init()
    {
        cou=0;
        Max=0;
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,ll w)
    {
        cou++;
        edge[cou].to=v;
        edge[cou].w=w;
        edge[cou].next=head[u];
        head[u]=cou;
        ///cout<<u<<" "<<v<<" "<<w<<endl;
    }
    void build(int l,int r,int pos,int flag,int t)
    {
        if(t==2) Max=max(Max,pos+flag);
        if(l==r)
        {
            if(t==2) add(pos+flag,l,0LL);
            else add(l,pos+flag,0LL);
            return;
        }
        if(t==2)
        {
            add(pos+flag,(pos<<1)+flag,0LL);
            add(pos+flag,(pos<<1|1)+flag,0LL);
        }
        else
        {
            add((pos<<1)+flag,pos+flag,0LL);
            add((pos<<1|1)+flag,pos+flag,0LL);
        }
        int mid=(l+r)>>1;
        build(l,mid,pos<<1,flag,t);
        build(mid+1,r,pos<<1|1,flag,t);
    }
    void update(int L,int R,ll w,int l,int r,int pos,int flag,int u,int t)
    {
        if(L<=l&&r<=R)
        {
            if(t==2) add(u,pos+flag,w);
            else add(pos+flag,u,w);
            return;
        }
        int mid=(l+r)>>1;
        if(L<=mid) update(L,R,w,l,mid,pos<<1,flag,u,t);
        if(R>mid) update(L,R,w,mid+1,r,pos<<1|1,flag,u,t);
    }
    struct mmp
    {
        int s;
        ll dis;
        mmp() {}
        mmp(ll ss,ll d)
        {
            s=ss,dis=d;
        }
        bool operator <(const  mmp &x)const
        {
            return dis>x.dis;
        }
    };
    priority_queue<mmp>q;
    void dij(int s)
    {
        ans[s]=0LL;
        q.push(mmp(s,0LL));
        while(!q.empty())
        {
            mmp now=q.top();
            q.pop();
            int u=now.s;
            if(vis[u]) continue;
            vis[u]=1;
            for(int i = head[u]; i!=-1; i=edge[i].next)
            {
                int v=edge[i].to;
                ll w=edge[i].w;
                if(ans[v]>now.dis+w)
                {
                    ///cout<<u<<" "<<v<<" "<<ans[u]+w<<endl;
                    q.push(mmp(v,now.dis+w));
                    ans[v]=now.dis+w;
                }
            }
        }
    }
    int main()
    {
        int n,q,s;
        scanf("%d%d%d",&n,&q,&s);
        init();
        build(1,n,1,n+1,2);
        build(1,n,1,Max+1,3);
        for(int i=1; i<=q; i++)
        {
            int t;
            scanf("%d",&t);
            if(t==1)
            {
                int u,v;
                ll w;
                scanf("%d%d%lld",&u,&v,&w);
                add(u,v,w);
            }
            else
            {
                int u,l,r;
                ll w;
                scanf("%d%d%d%lld",&u,&l,&r,&w);
                if(t==2) update(l,r,w,1,n,1,n+1,u,t);
                else update(l,r,w,1,n,1,Max+1,u,t);
            }
        }
        for(int i=0;i<MAXN;i++) ans[i]=INF;
        dij(s);
        for(int i=1; i<=n; i++)
        {
            if(ans[i]>=INF) cout<<"-1 ";
            else cout<<ans[i]<<" ";
        }
        cout<<endl;
        return 0;
    }
    线段树+搜索
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    数据库设计
    最近发生的事
    轻松搞定面试中的二叉树题目
    二叉树的遍历方法
    静态库Lib和动态库Dll
    深入理解C/C++数组和指针
    夜间日记
    typedef用法小结
    什么是静态链接库,什么是动态链接库
    线段树
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/6623270.html
Copyright © 2020-2023  润新知