1. 程式人生 > >786B Legacy (線段樹建圖+最短路)

786B Legacy (線段樹建圖+最短路)

B. Legacy

time limit per test

2 seconds memory limit per test256 megabytes inputstandard inputoutputstandard 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+1開始,第一顆線段樹每一個節點向它所代表的區間的所有點連線一條權值為0的邊,第二顆線段樹每一個節點所代表的區間的所有點向這個節點連線一條權值的0的邊,如下圖,這樣【L,R】向v新增邊的時候,就是第二顆線段樹【L,R】區間對應的節點編號向v連線一條w的邊,v向【L,R】新增邊的時候,就是v向第一顆線段樹【L,R】區間對應的節點編號連線一條w的邊,在圖上畫一畫一目瞭然,建兩顆線段樹又有點拆點的思想,邊數很多,用dij跑最短路,給了262144 kB記憶體,用了257052kb記憶體,好險

#include <bits/stdc++.h>
#define lson num << 1
#define rson num << 1 | 1
using namespace std;
typedef long long ll;
const int MAXN = 6e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Edge{
	int from,to;
	ll dist;
	Edge(int u,int v,ll d):from(u),to(v),dist(d){}
};
struct HeapNode{
	int u;
	ll d;
	HeapNode(int _u,ll _d):u(_u),d(_d){}
	bool operator < (const HeapNode& rhs) const{
		return d > rhs.d;
	}
};
priority_queue<HeapNode> Q;
struct Dijkstra{
	int n,m;
	vector<Edge> edges;
	vector<int> G[MAXN];
	bool done[MAXN];
	ll d[MAXN];
	int p[MAXN];
	void init(){
		for(int i = 1; i <= MAXN; i++) {
			G[i].clear();
        }
		edges.clear();
	}
	void AddEdge(int from,int to,ll dist) {
		edges.push_back(Edge(from,to,dist));
		m = edges.size();
		G[from].push_back(m - 1);
	}
	void dijkstra(int s) {
        while(!Q.empty()) Q.pop();
		memset(d,INF,sizeof(d));
		d[s] = 0;
		memset(done,0,sizeof(done));
		Q.push((HeapNode){s,0ll});
		while(!Q.empty()) {
			HeapNode x = Q.top();
			Q.pop();
			int u = x.u;
			if(done[u]){
				continue;
			}
			done[u] = true;
			for(int i = 0; i < G[u].size(); i++) {
				Edge& e = edges[G[u][i]];
				if(d[e.to] > d[u] + e.dist) {
					d[e.to] = d[u] + e.dist;
				    p[e.to] = G[u][i];
					Q.push((HeapNode){e.to,d[e.to]});
				}
			}
		}
	}
}dij;
struct node
{
    int l,r,id;
}tree[2][MAXN];
int idx;
void build(int num,int l,int r,int ty)
{
    tree[ty][num].id = ++idx;
    tree[ty][num].l = l;
    tree[ty][num].r = r;
    for(int i = l; i <= r; i++) {
        if(ty == 0) dij.AddEdge(tree[ty][num].id,i,0);
        else dij.AddEdge(i,tree[ty][num].id,0);
    }
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(lson,l,mid,ty);
    build(rson,mid + 1,r,ty);
}
void update(int num,int u,int l,int r,ll w,int ty)
{
    if(tree[ty][num].l == l && tree[ty][num].r == r) {
        if(ty == 0) dij.AddEdge(u,tree[ty][num].id,w);
        else dij.AddEdge(tree[ty][num].id,u,w);
        return;
    }
    int mid = (tree[ty][num].l + tree[ty][num].r) >> 1;
    if(r <= mid) update(lson,u,l,r,w,ty);
    else if(l > mid) update(rson,u,l,r,w,ty);
    else {
        update(lson,u,l,mid,w,ty);
        update(rson,u,mid + 1,r,w,ty);
    }
}
int main(void)
{
    int n,q,s;
    int op,u,v,l,r;
    ll w;
    while(scanf("%d %d %d",&n,&q,&s) != EOF) {
        idx = n;
        dij.init();
        build(1,1,n,0);
        build(1,1,n,1);
        while(q--) {
            scanf("%d",&op);
            if(op == 1) {
                scanf("%d %d %I64d",&u,&v,&w);
                dij.AddEdge(u,v,w);
            }
            else if(op == 2) {
                scanf("%d %d %d %I64d",&u,&l,&r,&w);
                update(1,u,l,r,w,0);
            }
            else {
                scanf("%d %d %d %I64d",&u,&l,&r,&w);
                update(1,u,l,r,w,1);
            }
        }
        dij.dijkstra(s);
        for(int i = 1; i <= n; i++) {
            if(dij.d[i] >= INF) dij.d[i] = -1ll;
        }
        for(int i = 1; i <= n; i++) {
            if(i == 1) printf("%I64d",dij.d[i]);
            else printf(" %I64d",dij.d[i]);
        }
        printf("\n");
    }
    return 0;
}