Codeforces Round #406 (Div. 2) D. 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 nq and s (1 ≤ n, q ≤ 1051 ≤ 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 vu and w where w is the cost of that plan (1 ≤ v, u ≤ n1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr and wwhere w is the cost of that plan (1 ≤ v ≤ n1 ≤ l ≤ r ≤ n1 ≤ 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.

题目链接:点击传送

题意:给你n个点,q个操作,s为起点

   t表示操作类型,1 v->u的权值为w ,2  v -> [l,r](区间所有点)的权值为w ,3 [l,r] -> v 的权值为w;

思路:

根据线段树的区间进行修改即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=2147483647;
const ll INF=1e17+10,mod=1e9+7;
struct is
{
    int v,next;
    ll w;
}edge[M*4];
int head[N*5],edg,vis[M];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    edg=0;
}
void add(int u,int v,ll w)
{
    edg++;
    edge[edg].v=v;
    edge[edg].w=w;
    edge[edg].next=head[u];
    head[u]=edg;
}
struct SGT
{
    int ls[N*5][2],rs[N*5][2],root[2];
    int tot;
    void build(int l,int r,int &pos,int type)
    {
        pos=tot++;
        if(l==r)
        {
            if(type)
                add(l,pos,0);
            else
                add(pos,l,0);
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,ls[pos][type],type);
        build(mid+1,r,rs[pos][type],type);
        if(type)
            add(ls[pos][type],pos,0),add(rs[pos][type],pos,0);
        else
            add(pos,ls[pos][type],0),add(pos,rs[pos][type],0);
    }
    void up(int v,int L,int R,ll c,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            //cout<<"add"<<v<<" "<<pos<<endl;
            add(pos,v,c);
            return;
        }
        int mid=(l+r)>>1;
        if(L<=mid)
            up(v,L,R,c,l,mid,ls[pos][1]);
        if(R>mid)
            up(v,L,R,c,mid+1,r,rs[pos][1]);
    }
    void down(int v,int L,int R,ll c,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            //cout<<"add"<<v<<" "<<pos<<endl;
            add(v,pos,c);
            return;
        }
        int mid=(l+r)>>1;
        if(L<=mid)
            down(v,L,R,c,l,mid,ls[pos][0]);
        if(R>mid)
            down(v,L,R,c,mid+1,r,rs[pos][0]);
    }
};
SGT tree;
struct mmp
{
    int s;
    ll dis;
    mmp(){}
    mmp(int ss,ll d){s=ss,dis=d;}
    bool operator <(const  mmp &b)const
    {
        return dis>b.dis;
    }
};
ll ans[N*5];
priority_queue<mmp>q;
void dij(int s)
{
    ans[s]=0;
    q.push(mmp(s,0LL));
    while(!q.empty())
    {
        mmp now = q.top();
        q.pop();
        if(vis[now.s])continue;
        vis[now.s]=1;
        for(int i = head[now.s]; i !=-1; i = edge[i].next)
        {
            int v=edge[i].v;
            ll w=edge[i].w;
            if(ans[v] > ans[now.s] + w)
            {
                q.push(mmp(v,ans[now.s]+w));
                ans[v]=ans[now.s]+w;
            }
        }
    }
}
int main()
{
    init();
    int n,q,s;
    scanf("%d%d%d",&n,&q,&s);
    for(int i=1;i<=n*5;i++)
        ans[i]=INF;
    tree.tot=n+1;
    tree.build(1,n,tree.root[1],1);
    tree.build(1,n,tree.root[0],0);
    for(int i=1;i<=q;i++)
    {
        int t,v,u,l,r;
        ll w;
        scanf("%d%d",&t,&v);
        if(t==1)
            scanf("%d%lld",&u,&w),add(v,u,w);
        else if(t==2)
            scanf("%d%d%lld",&l,&r,&w),tree.down(v,l,r,w,1,n,tree.root[0]);
        else
            scanf("%d%d%lld",&l,&r,&w),tree.up(v,l,r,w,1,n,tree.root[1]);
    }
    dij(s);
    for(int i=1;i<=n;i++)
    {
        if(ans[i]>=INF)printf("-1 ");
        else printf("%lld ",ans[i]);
    }
    printf("\n");
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/jhz033/p/6616675.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值