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.
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
- 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).
OutputIn 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() { int 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; } }
Codeforces 787D. Legacy 線段樹優化建圖+最短路