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:
- With a plan of this type you can open a portal from planet v to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- 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.
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).
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.
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
0 28 12
4 3 1 3 4 1 3 12 2 2 3 4 10 1 2 4 16
0 -1 -1 12
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.
见到好题就想写写博客(at:虽然是后来补的题),emmmm、、、、
题意:有n个点(编号1-n),q个询问,每个询问有3种操作:
操作1:u到v连一条权值为w
操作2:v可以到[l,r]内任意点权值为w
操作3:[l,r]内任意点可以到v权值为w
以上都为单向边,已知起点,求到其他所有点的最短距离,到达不了输出-1
题解:直接建边跑最短路---超时,爆内存
考虑用线段树的结构缩点,然后在新节点的基础上跑最短路就可以了。
缩点:先建一颗连通(父子连通)的log(n)层满二叉树,连上的边权值都为0(why?就是让ta连通啊~~~).然后操作2、3输入l,r,v时直接v与l,r的root相连即可(注意方向)
图建完了,直接跑堆优化的dijkstra就可以啦QAQ
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define ll long long
const int N=5000005;
using namespace std;
struct nodee{
int v;
ll w;
int next;
}e[N];
const ll INF=1e18;
int head[N],tot,Max;
bool vis[N];
ll dis[N];
int n,q,s;
void init()
{
tot=Max=0;
memset(head,-1,sizeof head);
}
void addedge(int u,int v,ll w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
void build(int rt,int l,int r,int flag,int t)
{
if(t==2) Max=max(Max,rt+flag);
if(l==r)
{
if(t==2) addedge(rt+flag,l,0LL);
else addedge(l,rt+flag,0LL);
return;
}
if(t==2)
{
addedge(rt+flag,(rt<<1)+flag,0LL);
addedge(rt+flag,(rt<<1|1)+flag,0LL);
}
else
{
addedge((rt<<1)+flag,rt+flag,0LL);
addedge((rt<<1|1)+flag,rt+flag,0LL);
}
int mid=(l+r)>>1;
build(lson,flag,t);
build(rson,flag,t);
}
void update(int rt,int l,int r,int a,int b,ll w,int flag,int u,int t)
{
if(a<=l&&b>=r)
{
if(t==2) addedge(u,rt+flag,w);
else addedge(rt+flag,u,w);
return;
}
int mid=(l+r)>>1;
if(a<=mid) update(lson,a,b,w,flag,u,t);
if(b>mid) update(rson,a,b,w,flag,u,t);
}
struct node{
int u;
ll di;
node(){}
node(int u,ll di):u(u),di(di){}
bool operator <(const node &a) const
{
return di>a.di;
}
};
priority_queue<node> que;
void dijkstra()
{
dis[s]=0LL;
que.push(node(s,0LL));
while(!que.empty())
{
node tmp=que.top();que.pop();
// cout<<tmp.u<<' ';
int u=tmp.u;
if(vis[u]) continue;
vis[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
ll w=e[i].w;
// cout<<v<<' ';
if(tmp.di+w<dis[v])
{
dis[v]=tmp.di+w;
que.push(node(v,dis[v]));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&q,&s);
init();
build(1,1,n,n+1,2);
build(1,1,n,Max+1,3);
while(q--)
{
int t,l,r,u,v;
ll w;
scanf("%d",&t);
if(t==1)
{
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w);
}
else
{
scanf("%d%d%d%lld",&u,&l,&r,&w);
if(t==2) update(1,1,n,l,r,w,n+1,u,t);
else update(1,1,n,l,r,w,Max+1,u,t);
}
}
// cout<<tot<<' '<<Max<<endl;
for(int i=0;i<N;i++)
dis[i]=INF;
dijkstra();
for(int i=1;i<=n;i++)
if(dis[i]==INF) dis[i]=-1;
for(int i=1;i<=n;i++)
{
if(i!=1) putchar(' ');
printf("%lld",dis[i]);
}
puts("");
return 0;
}