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

建立两棵线段树,对树上结点进行建边;注意点是图的点数要开8倍,INF要开大。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define PI acos(-1)
#define pn printf("\n");
using namespace std; int n,q,s;
const int maxn = 1e5+;
const int MAXN = maxn << ;
const ll INF = 1e18+;
int tol; // 单树结点数
int up[maxn << ], dn[maxn << ]; // 上面的树的编号, 下面的树的编号
int u, op;
int u_id[maxn]; // 下标为i的点在线段树中的编号
ll w; struct qnode
{
int v;
ll c;
qnode(int _v=,ll _c=):v(_v),c(_c){}
bool operator <(const qnode &r) const
{
return c > r.c;
}
};
struct Edge {
int v;
ll cost;
Edge(int _v=,ll _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
ll dist[MAXN]; void init()
{
tol = ;
memset(up, , sizeof up);
memset(dn, , sizeof dn);
for(int i=;i<MAXN;i++) E[i].clear();
tol = n*-;
} void Dijkstra(int n,int start)//点的编号从1开始
{
memset(vis, false, sizeof(vis));
for(int i=; i<=n; i++)
dist[i] = INF;
priority_queue <qnode> que;
while(!que.empty()) que.pop();
dist[start] = ;
que.push(qnode(start, ));
qnode tmp;
while(!que.empty())
{
tmp = que.top();
que.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i=; i<E[u].size(); i++)
{
int v = E[tmp.v][i].v;
ll cost = E[u][i].cost;
if(!vis[v] && dist[v] > dist[u]+cost)
{
dist[v] = dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
} void addedge(int u, int v, ll w)
{
E[u].push_back(Edge(v,w)); //cout << u << " -> " << v << " : " << w << endl;
} void build(int i,int b,int e,int pos, int *t)
{
if(b == e)
{
t[i] = i;
u_id[pos] = i;
return ;
} int mid = (b + e) / ;
if(pos <= mid) build(i << , b, mid, pos, t);
else build(i << | , mid + , e, pos, t); t[i] = i;
} void update(int i, int b, int e, int l, int r, bool mode)// mode=0: 点到区间, 1: 区间到点
{
if(b >= l && e <= r)
{
if(mode)
{
addedge(up[i], u_id[u]+tol, w); // 上面的树的区间的编号 到 下面的树的点的编号 建边
}
else
{
addedge(u_id[u], dn[i], w); // 上面的树的点的编号 到 下面的树的区间的编号 建边
}
return ;
} int mid = (b + e) >> ;
if(r <= mid) update(i << , b, mid, l, r, mode);
else if(l > mid) update(i << | , mid+, e, l, r, mode);
else
{
update(i << , b, mid, l, r, mode);
update(i << | , mid+, e, l, r, mode);
}
} void build_edge()
{
int vis[MAXN] = {};
queue <int> que;
for(int i=;i<=n;i++)
{
que.push(u_id[i]);
addedge(u_id[i], u_id[i] + tol, );
addedge(u_id[i] + tol, u_id[i], ); // 点到点建双向边
}
while(!que.empty())
{
int u = que.front();
que.pop(); addedge(u, u >> , ); // 从下到上建单向边
addedge( (u>>) + tol, u+tol, ); // 从上到下建单向边
if( (u>>) > && !vis[u>>] )
{
vis[u>>] = ;
que.push( u>> ); // 结点上移
}
}
} int main()
{
while(~scanf("%d%d%d", &n,&q,&s))
{
init();
int l, r;
for(int i=;i<=n;i++)
build(, , n, i, up); // 对上面的树的节点赋值
for(int i=;i<=tol;i++)
dn[i] = up[i] + tol; // 下面的树的节点的值 赋为 上面的树对应的点+tol
build_edge(); // 分别建从下到上、从上到下的单向边;点对点的双向边 while(q--)
{
scanf("%d", &op);
if(op == ) // 点到点建边
{
int v;
scanf("%d%d%lld", &u, &v, &w);
addedge(u_id[u], u_id[v]+tol, w); // 从上面的树的叶子结点指到下面树叶子结点
}
else if(op == ) // 点到区间
{
scanf("%d%d%d%lld", &u, &l, &r, &w);
update(, , n, l, r, );
}
else // 区间到点
{
scanf("%d%d%d%lld", &u, &l, &r, &w);
update(, , n, l, r, );
}
} Dijkstra(tol<<, u_id[s]); // 总点数:两个线段树的节点数*2, 起点:下标为s的点在上面的树中的编号
for(int i=;i<=n;i++)
{
if(i > ) printf(" ");
printf("%lld", dist[u_id[i] + tol] == INF ? - : dist[u_id[i] + tol] );
} pn;
}
}

Codeforces Round #406 (Div. 2) 787-D. Legacy的更多相关文章

  1. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  2. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  3. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E

    http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...

  6. 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D

    http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...

  7. 有向图博弈+出度的结合 Codeforces Round #406 (Div. 2) C

    http://codeforces.com/contest/787/problem/C 题目大意:有一个长度为n的环,第1个位置是黑洞,其他都是星球.已知在星球上(不含第一个黑洞)有一位神.有两个人, ...

  8. Codeforces Round #406 (Div. 1)

    B题打错调了半天,C题想出来来不及打,还好没有挂题 AC:AB Rank:96 Rating:2125+66->2191 A.Berzerk 题目大意:有一个东东在长度为n的环上(环上点编号0~ ...

  9. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

随机推荐

  1. 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。

    本文转自http://www.cnblogs.com/davidwang456/p/4090058.html 感谢作者 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并 ...

  2. Javascript:使用jQuery提交Form表单

    DEMO说明一切: // this is the id of the form $("#idForm").submit(function() { var url = "p ...

  3. Androd自己定义控件(三)飞翔的小火箭

    在前面的自己定义控件概述中已经跟大家分享了Android开发其中自己定义控件的种类. 今天跟大家分享一个非主流的组合控件. 我们在开发其中,难免须要在不同的场合中反复使用一些控件的组合.而Java的最 ...

  4. SSH框架之Struts(3)——Struts的执行流程之核心方法

    上篇讲了Tomcat实例化一个单例的ActionServlet.依据web.xml配置文件做好对应的初始化工作. 这时client产生一个.do结尾的request请求,採用get/post方式提交之 ...

  5. javac compiling error ( mising package)

    javac 编译java源文件时,提示 package does not exist 的错误 Test.java import java.security.MessageDigest; import ...

  6. Graphics简单汇总

    1.主页面布局文件 activity_main.xml(仅仅有2个button按钮) <?xml version="1.0" encoding="utf-8&quo ...

  7. ListView无障碍识别整个listView,不识别item,设置了setContentDescription也没实用

    点击ListView的时候.无障碍识别到的是整个listView,不会读点击的那个item. 解决的方法是在getView里手动设置: <span style="font-size:1 ...

  8. 1.ArcGis几何图形之几何计算

    /// <summary> /// 检测几何图形A是否包含几何图形B /// </summary> /// <param name="pGeometryA&qu ...

  9. C99新增内容之变长数组(VLA)

    我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS ...

  10. 另一种压缩图片的方法---Machine learning 之 PCA(Principle Component Analysis)

    PCA最主要的用途是用来减少特征向量的数目,N个特征向量 减小到 K个特征向量.如果为了可视化,k可以使3 或者 2.这样可以加速算法的学习速度. PCA用来压缩图像同一有效. 具体方式以及原理在gi ...