codeforces 786B Legacy 线段树建图最短路

B. 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.

扫描二维码关注公众号,回复: 4834178 查看本文章

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. u v w 点u向点v连权重为w的边

2. u l r w 点u向区间[l, r]的点均连一条权重为w的边

3. u l r w 区间[l, r]的点均连一条权重为w的边到u

建立两棵线段树,第一棵线段树的父节点向子节点连接权重为0的边,叶结点向图中节点连权重为0的边,另一棵子节点向父节点连权重为0的边,图中节点向叶子节点连权重为0的边,对于第二种操作,则只需u向第一棵线段树中[l, r]区间对应的点连边即可,因此每次操作将o(n)的边降为了o(1)。图片来自这里 (图中未画出叶结点和原图中的边)

#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define INF 200000000000000
typedef  long long ll;
const int maxn = 6e5 + 10;
int n, q, s;
int rts, num[2][maxn], vis[maxn];
ll dist[maxn];
vector<pair<int, ll> > G[maxn];

void build(int l, int r, int o, int x) {
    rts++;
    num[x][o] = rts;
    if (l == r) {
        if (x == 0) G[num[x][o]].push_back({l, 0});
        else G[l].push_back({num[x][o], 0});
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, o * 2, x);
    build(mid + 1, r, o * 2 + 1, x);
    if (x == 0) {
        G[num[x][o]].push_back({num[x][o * 2], 0});
        G[num[x][o]].push_back({num[x][o * 2 + 1], 0});
    }
    else {
        G[num[x][o * 2]].push_back({num[x][o], 0});
        G[num[x][o * 2 + 1]].push_back({num[x][o], 0});
    }
}

void update(int fr, int L, int R, int l, int r, int o, int x, ll w) {
    if (L <= l && r <= R) {
        if (x == 0) {
            G[fr].push_back({num[x][o], w});
        }
        else {
            G[num[x][o]].push_back({fr, w});
        }
        return;
    }
    int mid = (l + r) / 2;
    if (L <= mid) update(fr, L, R, l, mid, o * 2, x, w);
    if (R > mid) update(fr, L, R, mid + 1, r, o * 2 + 1, x, w);
}

void spfa() {
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    dist[s] = 0;
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].first;
            ll w = G[u][i].second;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                if (!vis[v]) {
                    Q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &q, &s);
    rts = n;
    memset(vis, 0, sizeof(vis));
    build(1, n, 1, 0);
    build(1, n, 1, 1);
    for (int i = 1; i <= rts; i++) dist[i] = INF;
    int t, u, v, l, r, w;
    for (int i = 0; i < q; i++) {
        scanf("%d", &t);
        if (t == 1) {
            scanf("%d%d%I64d", &u, &v, &w);
            G[u].push_back({v, w});
        }
        else if (t == 2) {
            scanf("%d%d%d%I64d", &u, &l, &r, &w);
            update(u, l, r, 1, n, 1, 0, w);
        }
        else {
            scanf("%d%d%d%I64d", &u, &l, &r, &w);
            update(u, l, r, 1, n, 1, 1, w);
        }
    }
    spfa();
    for (int i = 1; i <= n; i++)
        if (dist[i] == INF) printf("-1 ");
        else printf("%I64d ", dist[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KIDGIN7439/article/details/83623451