1. 程式人生 > >787D (線段樹優化建圖+最短路)

787D (線段樹優化建圖+最短路)

Legacy

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 wis 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

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星球開始到其他星球的距離。

q條路徑有可能有這麼幾種形式:

一:從一點u到v,費用為W

二:從一點u到區間[l,r]之間的任意一個星球,費用為w

三:從[l,r]之間任意一個星球到v,費用為w

思路:

不難想到直接從一個點向其他點跑出最短路就完事了。但是,觀察資料我們不難發現,這樣邊數一下就爆炸了。考慮到是區間建邊,可以採用線段樹優化建圖的策略,優化邊數。

簡單地介紹一下線段樹優化建圖的策略:

我們以一段1-n的區段為例子:

我們建立兩棵樹,一棵代表入,一棵代表入:

入樹上所有的節點向他們的父親節點連邊,出樹從父節點向子節點連邊,所有的葉子節點之間連線上雙向邊(這些邊的權值都是0)。這樣我們可以試著推導一下,假設從v點向[l,r]之間連邊,那麼從v到[l,r]之間的點u就會經過v點,u所在的區間的節點再到達u,而如果從[l,r]到v連邊,那麼從[l,r]之間的u點就會進過u所在的區間節點到v節點,而葉子節點之間連線雙向邊也是保證了到達一個節點之後可以繼續向其他節點走。

AC程式碼:

#include<cstdio>
#include<iostream>
#include<queue>
#define md(l,r) (((l)+(r))>>1)
#define rson(x) ((x)<<1|1)
#define lson(x) ((x)<<1)
typedef int Int;
#define int long long
using namespace std;
typedef long long LL;
const int size=1e5+5;
const int maxn=1e6+5;
const int inf=1e18+5;
struct node{
	int l,r;
	int id;
};
struct Edge{
	int u,v,w;
	Edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){}
};
struct roar{
	int id,w;
	roar(int id=0,int w=0):id(id),w(w){}
	friend bool operator<(roar a,roar b)
	{
		return a.w>b.w;
	}
};
priority_queue<roar> q;
vector<Edge> edge[maxn];
int dis[maxn],vis[maxn];
int cnt=0;
void AddEdge(int u,int v,int w)
{
	edge[u].push_back(Edge(u,v,w));
}
int build(int k,int l,int r,node tree[],int *no,bool type)
{
	tree[k].l=l,tree[k].r=r;tree[k].id=cnt++;
	if(l==r)
	{
		return no[l]=tree[k].id;
	}
	int mid=md(l,r);
	if(type)//從上向下建邊
	AddEdge(tree[k].id,build(lson(k),l,mid,tree,no,type),0),
	AddEdge(tree[k].id,build(rson(k),mid+1,r,tree,no,type),0);
	else//從下向上建邊
	AddEdge(build(lson(k),l,mid,tree,no,type),tree[k].id,0),
	AddEdge(build(rson(k),mid+1,r,tree,no,type),tree[k].id,0);
	return tree[k].id;
}
void query(int k,int l,int r,int faSno,int w,node tree[],bool type)
{
	if(tree[k].l>=l&&tree[k].r<=r)
	{
		if(type) AddEdge(faSno,tree[k].id,w);
		else AddEdge(tree[k].id,faSno,w);
		return ;
	}
	int mid=md(tree[k].l,tree[k].r);
	if(r<=mid) query(lson(k),l,r,faSno,w,tree,type);
	else if(l>=mid+1) query(rson(k),l,r,faSno,w,tree,type);
	else query(lson(k),l,mid,faSno,w,tree,type),query(rson(k),mid+1,r,faSno,w,tree,type);
}
void Dijkstra(int beg)
{
	fill(dis,dis+maxn,inf);
	fill(vis,vis+maxn,inf);
	while(!q.empty()) q.pop();
	q.push(roar(beg,0)),dis[beg]=0;
	while(!q.empty())
	{
		roar s=q.top();
		q.pop();
		int id=s.id;
		if(dis[id]!=s.w) continue;
		for(int i=0;i<edge[id].size();i++)
		{
			if(dis[edge[id][i].v]>dis[id]+edge[id][i].w)
			{
				dis[edge[id][i].v]=dis[id]+edge[id][i].w;
				q.push(roar(edge[id][i].v,dis[edge[id][i].v]));
			}
		}
	}
}
node intree[size<<2],outtree[size<<2];
int inno[size],outno[size];
Int main()
{
	int n,q,s;
	scanf("%lld%lld%lld",&n,&q,&s);
	build(1,1,n,intree,inno,0);
	build(1,1,n,outtree,outno,1);
	for(int i=1;i<=n;i++) AddEdge(inno[i],outno[i],0),AddEdge(outno[i],inno[i],0);
	for(int i=1;i<=q;i++)
	{
		int op;
		scanf("%lld",&op);
		if(op==1)
		{
			int u,v,w;
			scanf("%lld%lld%lld",&u,&v,&w);
			AddEdge(inno[u],outno[v],w);
		}
		else if(op==2)
		{
			int v,l,r,w;
			scanf("%lld%lld%lld%lld",&v,&l,&r,&w);
			query(1,l,r,inno[v],w,outtree,1);
		}
		else
		{
			int v,l,r,w;
			scanf("%lld%lld%lld%lld",&v,&l,&r,&w);
			query(1,l,r,outno[v],w,intree,0);
		}
	}
	Dijkstra(inno[s]);
	for(int i=1;i<=n;i++)
	{
		if(dis[outno[i]]!=inf)printf("%lld ",dis[outno[i]]);
		else printf("-1 ");
	}
	cout<<endl;
}