codeforces 787D (线段树建图+dij)

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 ≤ 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 vu and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr 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

Copy

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

Copy

0 28 12 

input

Copy

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

output

Copy

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个点,有一个起始点,你要求出从起始点到其他点的最短路,那么这就是一个很简单的最短路问题,但是麻烦的地方在建图,普通的建图这里肯定是不行的。因为这里有三种操作,第一 从u到v 加一条边,权值 为w 第二 从u到 l,r 的点都可以,权值为w ,第三 从  l,r 到u点,权值为w 。那么对于这种区间,我们可以用线段树建图的方法,

这里方法是:

建立两颗线段树

A线段树每个节点和其父亲节点之间有一条边,边权为0

B线段树每个节点和其两个儿子节点之间有一条边,边权为0

B线段树的每个叶节点和A线段树的每个叶节点之间有一条边,边权为0

对于每一个op==1 的操作,我们建一条从A线段树叶节点到B线段树叶节点的边,边权为w

对于每一个op==2的操作,我们建一条从A线段树叶节点到B线段树区间节点的边,边权为w 因为B线段树是从上向下建,那么就相当于u节点可以到B区间节点的区间内的每一个节点。

对于每一个op==3 的操作,我们建一条从A线段树区间节点到B线段树叶子节点的边,边权为w 因为A线段树我们是从下向上建,那么就相当于A区间节点内的每一个点都可以到达B点的叶子节点。

最后跑一个最短路就可以了。

#include<bits/stdc++.h>
#define lson (i<<1)
#define rson (i<<1|1)

using namespace std;
typedef long long ll;
typedef pair< ll, int > pli;
const ll inf =1e18+5;
const int N =1e5+5;

struct node
{
    int l,r;
    int num;
}ta[N<<2],tb[N<<2];

int pa[N],pb[N];
struct node1
{
    ll w;
    int v;
    node1(ll _w,int _v):w(_w),v(_v){}
};
vector< node1 >ve[N*8];
int vis[N*8];
ll dis[N*8];
int tot;


void builda(int i,int l,int r)// 自下向上建边
{
    ta[i].l=l; ta[i].r=r; ta[i].num=++tot;
    if(ta[i].l==ta[i].r){
        pa[l]=ta[i].num;
        return ;
    }
    int mid=(ta[i].l+ta[i].r)>>1;
    builda(lson,l,mid);
    builda(rson,mid+1,r);
    ve[ta[lson].num].push_back(node1(0,ta[i].num));
    ve[ta[rson].num].push_back(node1(0,ta[i].num));
}

void updatea(int i,int l,int r,int num,ll w)
{
    if(ta[i].l==l&&ta[i].r==r){ // cong l,r dao u
        ve[ta[i].num].push_back(node1(w,num)); // 从a线段树的区间向b线段树的底部建一条边
        return ;
    }
    int mid=(ta[i].l+ta[i].r)>>1;
    if(r<=mid) updatea(lson,l,r,num,w);
    else if(l>mid) updatea(rson,l,r,num,w);
    else{
        updatea(lson,l,mid,num,w);
        updatea(rson,mid+1,r,num,w);
    }
}

void buildb(int i,int l,int r)  // 自上向下建边
{
    tb[i].l=l; tb[i].r=r; tb[i].num=++tot;
    if(tb[i].l==tb[i].r){
        pb[l]=tb[i].num;
        ve[tb[i].num].push_back(node1(0,pa[l]));  //从b线段树的底部向a线段树的底部建一条0遍表示两个点在逻辑上是一个点
        //cout<<"**** u "<<tb[i].num<<" "<<pa[l]<<endl;
        return ;
    }
    int mid=(tb[i].l+tb[i].r)>>1;
    buildb(lson,l,mid);
    buildb(rson,mid+1,r);
    ve[tb[i].num].push_back(node1(0,tb[lson].num));
    ve[tb[i].num].push_back(node1(0,tb[rson].num));
}

void updateb(int i,int l,int r,int num,ll w)
{
    if(tb[i].l==l&&tb[i].r==r){
        ve[num].push_back(node1(w,tb[i].num)); // 从a线段树的底部向b线段树的 区间建一条边
        //cout<<"****u "<<num<<" "<<tb[i].num<<endl;
        return ;
    }
    int mid=(tb[i].l+tb[i].r)>>1;
    if(r<=mid) updateb(lson,l,r,num,w);
    else if(l>mid) updateb(rson,l,r,num,w);
    else{
        updateb(lson,l,mid,num,w);
        updateb(rson,mid+1,r,num,w);
    }
}

int n,m,q,s;

void dij(int st)
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=8*n;i++) dis[i]=inf;
    dis[st]=0;
    pli tmp;

    priority_queue< pli,vector<pli> ,greater<pli> >q;
    q.push(pli(0,st));

    int u,v;
    while(!q.empty()){
        tmp=q.top(); q.pop();
        u=tmp.second;
        if(tmp.first>dis[u]) continue;
        vis[u]=1;
        //cout<<"****** u "<<u<<" "<<dis[u]<<endl;
        for(int i=0;i<ve[u].size();i++)
        {
            v=ve[u][i].v;
            if(vis[v]) continue;
            if(dis[v]>dis[u]+ve[u][i].w){
                dis[v]=dis[u]+ve[u][i].w;
                //cout<<"*** v "<<v<<" "<<dis[v]<<" "<<ve[u][i].w<<endl;
                q.push(pli(dis[v],v));
            }
        }

    }

    for(int i=1;i<=n;i++){
        if(dis[pa[i]]!=inf){
            printf("%lld ",dis[pa[i] ]);
        }
        else printf("-1 ");
    }

    return ;
}

int main()
{
    int op,l,r,u,v;
    ll w;
    scanf("%d %d %d",&n,&m,&s);
    int rta=1;
    builda(1,1,n);
    int rtb=1;
    //cout<<"rtb "<<rtb<<endl;
    buildb(rtb,1,n);

    while(m--)
    {
        scanf("%d",&op);
        if(op==1){
            scanf("%d %d %lld",&u,&v,&w);
            ve[pa[u]].push_back(node1(w,pb[v]));
        }
        else if(op==2){ // 从 u 到 l,r
            scanf("%d %d %d %lld",&u,&l,&r,&w);
            updateb(rtb,l,r,pa[u],w);
        }
        else if(op==3){  // 从 l,r到u
            scanf("%d %d %d %lld",&u,&l,&r,&w);
            updatea(rta,l,r,pb[u],w);
        }
    }

    dij(pa[s]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81301334
今日推荐