codeforces787D. 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 ≤ 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个点,m个传送的方案,起点为s.

传送方案有3种。

1、从u传送到v费用为w

2、从u传送到[ l , r ]范围上任意一点,费用为w

3、从[l ,r ]范围上任意一点传送到u,费用为w.

现在询问到达每个点的最短路。

 

三、大致思路

肯定是建图跑一个最短路,但是按照正常的建边方法,在一个范围内连接边的话会导致最后边数过多超时。

所以要用线段树来辅助建图。如样例1可以建立下图的路径(仅画了前两条边):

利用线段树的节点,大幅度减少了重复边。

 

四、代码

#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 inf=0x3f3f3f3f;
const LL INF=1e17+10;


int n,q,s;
int ATreeSize;
/*-----------
最短路部分
-----------*/
typedef struct
{
	int v,next;
	LL cost;
}Edge;
typedef struct
{
	int v;
	LL cost;
}Node;
bool operator <(const Node &a,const Node &b)
{
	return a.cost>b.cost;
}

const int MAXN=(1000005<<2),MAXM=(1000005<<2);
int head[MAXN];
LL dis[MAXN];
Edge e[MAXM];
bool vis[MAXN];
int ind;

void addEdge(int from,int to,LL cost)
{
	e[ind].next=head[from];
	e[ind].v=to;
	e[ind].cost=cost;
	head[from]=ind++;
}

void Dijkstra(int start)
{
    memset(vis,false,sizeof(vis));
	for(int i=1;i<=MAXN-1;i++)dis[i]=INF;
	Node t;
	priority_queue<Node>q;
	t.cost=0;
	dis[start]=0;
	t.v=start;
	q.push(t);
	while(!q.empty())
	{
		t=q.top();
		q.pop();
		if(vis[t.v])continue;
		vis[t.v]=true;
		for(int i=head[t.v];i!=-1;i=e[i].next)
		{
			if(!vis[e[i].v]&&dis[e[i].v]>dis[t.v]+e[i].cost)
			{
				Node temp;
				temp.v=e[i].v;
				temp.cost=e[i].cost+t.cost;
				dis[e[i].v]=dis[t.v]+e[i].cost;
				q.push(temp);
			}
		}
	}
}

/*------------
线段树
------------*/
void build(int i,int l, int r,int type,int extra)
{
	if(type==2)ATreeSize=max(ATreeSize,extra+i);
	if (l == r)
	{
		if(type==2)addEdge(i+extra,l,0);
		else if(type==3)addEdge(l,i+extra,0);
		return;
	}
	if(type==2)
    {
        addEdge(i+extra,(i<<1)+extra,0);
        addEdge(i+extra,(i<<1|1)+extra,0);
    }
    else
    {
        addEdge((i<<1)+extra,i+extra,0);
        addEdge((i<<1|1)+extra,i+extra,0);
    }
	int mid = (l + r) / 2;
	build(i << 1, l, mid,type,extra);
	build((i << 1) | 1, mid + 1, r,type,extra);
}

void updateEdge(int i,int l, int r,int L,int R,int u,LL w,int type,int extra)
{
    if(L==l&&r==R)
    {
        if(type==2)addEdge(u,extra+i,w);
        else addEdge(extra+i,u,w);
        return ;
    }
    int mid=(L+R)>>1;
    if(r<=mid) updateEdge(i<<1,l,r,L,mid,u,w,type,extra);
    else if(l>mid) updateEdge(i<<1|1,l,r,mid+1,R,u,w,type,extra);
    else
    {
        updateEdge(i<<1,l,mid,L,mid,u,w,type,extra);
        updateEdge(i<<1|1,mid+1,r,mid+1,R,u,w,type,extra);
    }
}


void readAndBuild()
{
    scanf("%d%d%d",&n,&q,&s);
    ATreeSize=0;
    memset(head,-1,sizeof(head));
    ind=0;
    build(1,1,n,2,n);
    build(1,1,n,3,ATreeSize);

    while(q--)
    {
        int t;
        scanf("%d",&t);
        if(t==1)
        {
            int u,v;LL w;
            scanf("%d %d %lld",&u,&v,&w);
            addEdge(u,v,w);
        }
        else
        {
            int u,l,r;LL w;
            scanf("%d %d %d %lld",&u,&l,&r,&w);
            if(t==2)
            {
                updateEdge(1,l,r,1,n,u,w,2,n);
            }
            else
            {
                updateEdge(1,l,r,1,n,u,w,3,ATreeSize);
            }
        }
    }
}
int main()
{
    readAndBuild();
    Dijkstra(s);
    for(int i=1;i<=n;i++)
    {
        if(dis[i]!=INF)printf("%lld ",dis[i]);
        else printf("-1 ");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值