博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 787D. Legacy 线段树优化建图+最短路
阅读量:5064 次
发布时间:2019-06-12

本文共 4993 字,大约阅读时间需要 16 分钟。

output
standard 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:

  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 nq 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 vu and w where w is the cost of that plan (1 ≤ v, u ≤ n1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr and w where w is the cost of that plan (1 ≤ v ≤ n1 ≤ l ≤ r ≤ n1 ≤ 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.

 

题意:和BZOJ3073很像,当然这里是有向图以及好像边权不一样一点。

题解:线段树优化建图+最短路。今天偶然翻别人博客看到的题,之前暑假多校也有一个,就是给你两个区间(a,b)->(c,d)连边然后跑单源最短路,在被数据结构降智后现在当然知道这要用线段树优化了QAQ。虽然建图方式还是很玄学但我们可以这样思考,首先建两颗线段树,一颗是出树一颗是入树,显然你可以从出树向入树连边,同时跑单源最短路显然你是从出树的某个叶子结点出发的,那么不难想到我们要向他们的父亲连边,反之入树要跑到某个叶子,所以父亲向儿子连边。同时入树的叶子连向出树的叶子表明可以再从出树走。然后对于这道题的三个操作再连边,这个就是类似区间更新搞一下就好了。然后答案就是入树中各个点的值,其中要特判起点。然后我一开始RE41了好几次,不懂,都已经开到1e5+7<<4了,感觉没爆,直接开到2e5就过了。。玄学。至于为什么BZOJ的题解多读入又简单我不做???向权限题势力低头。

#include
#define ll long long#define ls x<<1#define rs x<<1|1#define pb push_back#define _mp make_pair#define ldb long doubleusing 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
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<
<
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<
<

  

 

转载于:https://www.cnblogs.com/intwentieth/p/9746028.html

你可能感兴趣的文章
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Windows Phone Marketplace 发布软件全攻略
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
语义web基础知识学习
查看>>
hexo个人博客添加宠物/鼠标点击效果/博客管理
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
关于WPF的2000件事 02--WPF界面是如何渲染的?
查看>>
单元测试、、、
查看>>
SVN使用教程总结
查看>>
JS 浏览器对象
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
虚拟中没有eth0
查看>>
Unity 3D游戏开发学习路线(方法篇)
查看>>
BZOJ2049[Sdoi2008]Cave 洞穴勘测(LCT模板)
查看>>
vuex插件
查看>>
2011年12月09日
查看>>
[ZJOI2007]棋盘制作 【最大同色矩形】
查看>>
合并单元格
查看>>
swift-初探webView与JS交互
查看>>