Codeforces 787D. Legacy 线段树优化建图+最短路
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.
![](http://codeforces.com/predownloaded/ba/1b/ba1b063d9f3f1edc4550fe3a21159354e74734c9.png)
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.
题意:和BZOJ3073很像,当然这里是有向图以及好像边权不一样一点。
题解:线段树优化建图+最短路。今天偶然翻别人博客看到的题,之前暑假多校也有一个,就是给你两个区间(a,b)->(c,d)连边然后跑单源最短路,在被数据结构降智后现在当然知道这要用线段树优化了QAQ。虽然建图方式还是很玄学但我们可以这样思考,首先建两颗线段树,一颗是出树一颗是入树,显然你可以从出树向入树连边,同时跑单源最短路显然你是从出树的某个叶子结点出发的,那么不难想到我们要向他们的父亲连边,反之入树要跑到某个叶子,所以父亲向儿子连边。同时入树的叶子连向出树的叶子表明可以再从出树走。然后对于这道题的三个操作再连边,这个就是类似区间更新搞一下就好了。然后答案就是入树中各个点的值,其中要特判起点。然后我一开始RE41了好几次,不懂,都已经开到1e5+7<<4了,感觉没爆,直接开到2e5就过了。。玄学。至于为什么BZOJ的题解多读入又简单我不做???向权限题势力低头。
#include<bits/stdc++.h> #define ll long long #define ls x<<1 #define rs x<<1|1 #define pb push_back #define _mp make_pair #define ldb long double using namespace std; const int maxn=2e5+7; const ll inf=1e18; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } ll fir[maxn<<4],nxt[maxn<<4],to[maxn<<4]; ll v[maxn<<4]; int pos[maxn<<2]; int vis[maxn<<4]; ll cnt,tot; int n,m,s; ll dis[maxn<<4]; struct Node { ll w; int pl; friend bool operator<(Node a,Node b) { return a.w>b.w; } Node(){} Node(ll vv,int y){w=vv;pl=y;} }; priority_queue<Node>que; void add_e(ll x,ll y,ll w) { ++cnt;nxt[cnt]=fir[x];fir[x]=cnt;to[cnt]=y;v[cnt]=w; } void build(int x,int l,int r,int flag,ll w) { if(l==r) { if(!flag)add_e(x+4*n,x,0); else pos[l]=x; return; } int mid=(l+r)>>1; build(x<<1,l,mid,flag,w); build(x<<1|1,mid+1,r,flag,w); if(!flag)add_e(ls,x,0),add_e(rs,x,0); else add_e(x+4*n,(ls)+4*n,0),add_e(x+4*n,(rs)+4*n,0); } void update(int x,int l,int r,int L,int R,int M,int flag,ll w) { if(L<=l&&r<=R) { if(!flag) { add_e(pos[M],x+4*n,w); } else { add_e(x,pos[M]+4*n,w); } return; } int mid=(l+r)>>1; if(L<=mid)update(ls,l,mid,L,R,M,flag,w); if(R>mid)update(rs,mid+1,r,L,R,M,flag,w); } void dij(int x) { for(int i=1;i<=n*10;i++)dis[i]=inf; memset(vis,0,sizeof(vis)); dis[x]=0; while(!que.empty())que.pop(); que.push(Node(0ll,x)); while(!que.empty()) { Node vv=que.top();que.pop(); // cout<<vv.pl<<endl; if(vis[vv.pl])continue; vis[vv.pl]=1; for(int i=fir[vv.pl];i;i=nxt[i]) { int tmp=to[i]; // cout<<tmp<<endl; if(dis[tmp]>dis[vv.pl]+v[i]) { dis[tmp]=dis[vv.pl]+v[i]; que.push(Node(dis[tmp],tmp)); } } } } int main() { scanf("%d%d%d",&n,&m,&s); memset(fir,0,sizeof(fir)); memset(pos,0,sizeof(pos)); cnt=0; build(1,1,n,0,0); build(1,1,n,1,0); int pp,qq,rr,ss,gg; ll fv; for(int i=1;i<=m;i++) { scanf("%d",&pp); if(pp==1) { qq=read();rr=read();fv=read(); add_e(pos[qq],pos[rr]+4*n,fv); } else if(pp==2) { qq=read();rr=read();gg=read();fv=read(); update(1,1,n,rr,gg,qq,0,fv); } else { qq=read();rr=read();gg=read();fv=read(); update(1,1,n,rr,gg,qq,1,fv); } } dij(pos[s]); for(int i=1;i<=n;i++) { if(i==s)printf("0"); else if(dis[pos[i]+4*n]!=inf)printf("%I64d",dis[pos[i]+4*n]); else printf("-1"); if(i!=n)printf(" "); else printf("\n"); /*if(dis[pos[i]]!=inf) { printf("%lld ",dis[pos[i]]); } else printf("-1 ");*/ //cout<<dis[pos[i]+4*n]<<endl; } }