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:
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 n, q 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 v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r 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
#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 Q;
struct Dijkstra{
int n,m;
vector edges;
vector 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;
}