Time limit 2000 ms
Memory limit 262144 kB
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 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.
翻译来源——洛谷
在宇宙中一共有n个星球标号为1到n。Rick现在身处于标号为s的星球。众所周知,Rick有一个传送枪,他用这把枪可以制造出一个从他所在的星球通往其他星球(也包括自己所在的星球)的单行道路。但是由于他还在用免费版,因此这把枪的使用是有限制的。
默认情况下他不能用这把枪开启任何传送门。在网络上有q个售卖这些传送枪的使用方案。每一次你想要实施这个方案时你都可以购买它,但是每次购买后只能使用一次。每个方案的购买次数都是无限的。
网络上一共有三种方案可供购买: 1.开启一扇从星球v到星球u的传送门; 2.开启一扇从星球v到标号在[l,r]区间范围内任何一个星球的传送门。(即这扇传送门可以从一个星球出发通往多个星球) 3.开启一扇从标号在[l,r]区间范围内任何一个星球到星球v的传送门。(即这扇传送门可以从多个星球出发到达同一个星球)
对于每一个星球(包括地球本身)Rick想要知道从地球到那个星球所需的最小钱数。
题目分析
线段树优化建边的经典题
对于一段连续的区间,会很自然地联想到线段树
相当于将图中一段连续编号的结点 可以用 线段树的一个结点表示
由于是有向图,需要建两棵有向线段树,线段树每条边边权为0
一棵是边由父亲指向子节点(向下建边),另一棵则相反(向上建边)
对于一个单独节点向一段区间连边,就从这个单独节点向向下建边的线段树结点连边,边权为给定参数
反之即相反建边
为什么要这样做呢,可以结合下面两张图理解
对于结点
x
x
x到区间
[
5
,
6
]
[5,6]
[5,6]的连边,我们应该这样建
由于线段树边权为0,那么x就可以顺着线段树向下的边更新到每个单独节点,而不会影响其他结点
加入我们像这样连边,那么
x
x
x到达区间
[
5
,
6
]
[5,6]
[5,6]后还会继续向上更新到其他区间,显然错误
相应的对于一段区间向一个单独节点 x x x连边就应该向下面这样
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
typedef long long lt;
#define pir pair<lt,lt>
#define mkp(x,y) make_pair(x,y)
lt read()
{
lt x=0,f=1;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return x*f;
}
const lt inf=1e16;
const int maxn=100010;
int n,m,s,cnt;
struct node{int v,nxt;lt dis;}E[maxn<<5];
int head[maxn*10],tot;
lt d[maxn*10];
int in[maxn<<2],out[maxn<<2],vis[maxn*10];
void add(int u,int v,lt dis)
{
E[++tot].nxt=head[u];
E[tot].v=v; E[tot].dis=dis;
head[u]=tot;
}
void build(int s,int t,int p)
{
if(s==t){ in[p]=out[p]=s; return;}
int mid=s+t>>1;
in[p]=++cnt; out[p]=++cnt;
build(s,mid,p<<1);build(mid+1,t,p<<1|1);
add(in[p],in[p<<1],0); add(in[p],in[p<<1|1],0);
add(out[p<<1],out[p],0); add(out[p<<1|1],out[p],0);
}
void qvet(int u,int ll,int rr,int s,int t,int p,lt dis,int tt)
{
if(ll<=s&&t<=rr){
if(tt==2) add(u,in[p],dis);
else if(tt==3) add(out[p],u,dis);
return;
}
int mid=s+t>>1;
if(ll<=mid) qvet(u,ll,rr,s,mid,p<<1,dis,tt);
if(rr>mid) qvet(u,ll,rr,mid+1,t,p<<1|1,dis,tt);
}
void dij()
{
for(int i=1;i<=cnt;++i) d[i]=inf; d[s]=0;
priority_queue<pir> q; q.push(mkp(0,s));
while(!q.empty())
{
int u=q.top().second; q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v;
if(d[v]>d[u]+E[i].dis)
{
d[v]=d[u]+E[i].dis;
q.push(mkp(-d[v],v));
}
}
}
}
int main()
{
n=cnt=read();m=read();s=read();
build(1,n,1);
for(int i=1;i<=m;++i)
{
int k=read(),u=read();
if(k==1)
{
int v=read();lt dis=read();
add(u,v,dis);
}
else
{
int ll=read(),rr=read();lt dis=read();
qvet(u,ll,rr,1,n,1,dis,k);
}
}
dij();
for(int i=1;i<=n;++i)
if(d[i]==inf) printf("-1 ");
else printf("%lld ",d[i]);
return 0;
}