https://www.luogu.org/problemnew/show/CF786B

CF786B Legacy

    • 104通过
    • 404提交
  • 题目来源CodeForces 786B
  • 评测方式RemoteJudge
  • 标签
  • 难度省选/NOI-
  • 时空限制2000ms / 256MB

提交  题解

  • 提示:收藏到任务计划后,可在首页查看。

最新讨论显示

推荐的相关题目显示

题意翻译

Rick和他的同事们做出了一种新的带放射性的婴儿食品(???根据图片和原文的确如此...),与此同时很多坏人正追赶着他们。因此Rick想在坏人们捉到他之前把他的遗产留给Morty。

在宇宙中一共有n个星球标号为1到n。Rick现在身处于标号为s的星球(地球)但是他不知道Morty在哪里。众所周知,Rick有一个传送枪,他用这把枪可以制造出一个从他所在的星球通往其他星球(也包括自己所在的星球)的单行道路。但是由于他还在用免费版,因此这把枪的使用是有限制的。

默认情况下他不能用这把枪开启任何传送门。在网络上有q个售卖这些传送枪的使用方案。每一次你想要实施这个方案时你都可以购买它,但是每次购买后只能使用一次。每个方案的购买次数都是无限的。

网络上一共有三种方案可供购买: 1.开启一扇从星球v到星球u的传送门; 2.开启一扇从星球v到标号在[l,r]区间范围内任何一个星球的传送门。(即这扇传送门可以从一个星球出发通往多个星球) 3.开启一扇从标号在[l,r]区间范围内任何一个星球到星球v的传送门。(即这扇传送门可以从多个星球出发到达同一个星球)

Rick并不知道Morty在哪儿,但是Unity将要通知他Morty的具体位置,并且他想要赶快找到通往所有星球的道路各一条并立刻出发。因此对于每一个星球(包括地球本身)他想要知道从地球到那个星球所需的最小钱数。

输入数据: 输入数据的第一行包括3个整数n,q和s(1<=n,q<=10^5,1<=s<=n)分别表示星球的数目,可供购买的方案数目以及地球的标号。

接下来的q行表示q种方案。 1.输入1 v u w表示第一种方案,其中v,u意思同上,w表示此方案价格。 2.输入2 v l r w表示第二种方案,其中v,l,r意思同上,w表示此方案价格。 2.输入3 v l r w表示第三种方案,其中v,l,r意思同上,w表示此方案价格。 (1<=v,u,l,r<=n,1<=w<=10^9)

输出格式: 输出一行用空格隔开的n个整数分别表示从地球到第i个星球所需的最小钱数。

说明: 在第一组测试样例里,Rick可以先购买第4个方案再购买第2个方案从而到达标号为2的星球.

感谢@radish布団 提供的翻译

题目描述

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 nn planets in their universe numbered from 11 to nn . Rick is in planet number ss (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 qq 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 vv to planet uu .
  2. With a plan of this type you can open a portal from planet vv to any planet with index in range [l,r][l,r] .
  3. With a plan of this type you can open a portal from any planet with index in range [l,r][l,r] to planet vv .

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 nn , qq and ss ( 1<=n,q<=10^{5}1<=n,q<=105 , 1<=s<=n1<=s<=n ) — number of planets, number of plans and index of earth respectively.

The next qq lines contain the plans. Each line starts with a number tt , type of that plan ( 1<=t<=31<=t<=3 ). If t=1t=1 then it is followed by three integers vv , uu and ww where ww is the cost of that plan ( 1<=v,u<=n1<=v,u<=n , 1<=w<=10^{9}1<=w<=109 ). Otherwise it is followed by four integers vv , ll , rr and ww where ww is the cost of that plan ( 1<=v<=n1<=v<=n , 1<=l<=r<=n1<=l<=r<=n , 1<=w<=10^{9}1<=w<=109 ).

输出格式:

In the first and only line of output print nn integers separated by spaces. ii -th of them should be minimum money to get from earth to ii -th planet, or -1−1 if it's impossible to get to that planet.

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

0 28 12

输入样例#2: 复制

4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16

输出样例#2: 复制

0 -1 -1 12

说明

In the first sample testcase, Rick can purchase 44 th plan once and then 22 nd plan in order to get to get to planet number 22 .

这是一道裸题,前提是你会线段树建边

有题目描述可以发现区间是连续的,什么东西是靠连续的区间维护吃饭的?

线段树,它是维护一个个区间的,并且任何一段连续的序列在线段树上分成的子序列不多,不会超过lg n个

自己想想如何维护吧

毕竟博主也是自己听到线段树后想通的,

————————————————————————————————罪恶的分割线

如果只有1,2操作,就只用构造一棵线段树,

但是多了3操作后就要两棵,

菜鸡组织不出语言如何写

#include<cstdio>
#include<queue>
#define ll long long
using namespace std;int read()
{int ret=0; char ch=getchar();while(ch<'0'||ch>'9') ch=getchar();while(ch>='0'&&ch<='9')ret=(ret<<1)+(ret<<3)+ch-'0',ch=getchar();return ret;
}const int N=3e6+5;
int n,T,tot,s;
ll d[N];
bool fl[N];
int cnt,to[N],nxt[N],he[N],w[N];struct B{int id; ll x; };
bool operator> (B i,B j)
{return i.x>j.x;
}
priority_queue<B,vector<B>,greater<B> >q;inline void add(int u,int v,int k)
{to[++cnt]=v,nxt[cnt]=he[u];he[u]=cnt,w[cnt]=k;
}struct A
{int id1[N],id2[N];void build1(int p,int l,int r){if(l==r) {id1[p]=l;return;}id1[p]=++tot;int mid=l+r>>1;build1(p<<1,l,mid);build1(p<<1|1,mid+1,r);add(id1[p],id1[p<<1],0);add(id1[p],id1[p<<1|1],0);}void build2(int p,int l,int r){if(l==r){id2[p]=l;return;}id2[p]=++tot;int mid=l+r>>1;build2(p<<1,l,mid);build2(p<<1|1,mid+1,r);add(id2[p<<1],id2[p],0);add(id2[p<<1|1],id2[p],0);}void find1(int p,int l,int r,int u,int x,int y,int k){if(l==x&&r==y) {add(u,id1[p],k);return;}int mid=l+r>>1;if(mid>=y) find1(p<<1,l,mid,u,x,y,k);else if(mid<x) find1(p<<1|1,mid+1,r,u,x,y,k);else find1(p<<1,l,mid,u,x,mid,k),find1(p<<1|1,mid+1,r,u,mid+1,y,k);}void find2(int p,int l,int r,int u,int x,int y,int k){if(l==x&&r==y){add(id2[p],u,k);return;}int mid=l+r>>1;if(mid>=y) find2(p<<1,l,mid,u,x,y,k);else if(mid<x) find2(p<<1|1,mid+1,r,u,x,y,k);else find2(p<<1,l,mid,u,x,mid,k),find2(p<<1|1,mid+1,r,u,mid+1,y,k);}
}tree;int main()
{n=read(),T=read(),s=read();tot=n;tree.build1(1,1,n);tree.build2(1,1,n);while(T--){int t=read();if(t==1) {int u=read(),v=read(),k=read();add(u,v,k);}elseif(t==2){int u=read(),l=read(),r=read(),k=read();tree.find1(1,1,n,u,l,r,k);}else{int u=read(),l=read(),r=read(),k=read();tree.find2(1,1,n,u,l,r,k);}}for(int i=1;i<=tot;i++) d[i]=1e18;q.push((B){s,d[s]=0});while(!q.empty()){while(!q.empty()&&fl[q.top().id]) q.pop();if(q.empty()) break;int u=q.top().id; q.pop(),fl[u]=1;for(int e=he[u];e;e=nxt[e]){int v=to[e];if(!fl[v]&&d[v]>d[u]+w[e]) q.push((B){v,d[v]=d[u]+w[e]});}}for(int i=1;i<=n;i++)printf("%lld%c",d[i]==1e18?-1:d[i],i==n?'\n':' ');return 0;
}

Luogu CF786B Legacy相关推荐

  1. CF786B Legacy(线段树优化建边模板 + 最短路)

    整理的算法模板合集: ACM模板 目录 线段树优化建边 题目传送门 由于本题的数据达到了1e5,所以如果直接全部暴力连边的话会达到O(n2)O(n^2)O(n2),时间包括内存都受不了.因此我们需要使 ...

  2. [CF786B]Legacy

    Legacy 题解 看到题目应该是容易想到最短路的,但是由于区间到单点与单点到区间的边我们需要想些办法来维护. 于是,我们就想到了通过虚点来进行维护.但总不能每个区间都建一个点,只能利用线段树来对各个 ...

  3. CF-786B(Legacy) 区间最短路

    题意 有三种边[u,v], [u, [L,R]], [[L,R], v],求从S出发到其他所有点的最短路 思路 线段树维护区间建图 #include <bits/stdc++.h> con ...

  4. CF786B Legacy

    第一次写线段树优化建边. 根据本题的要求,我们可以建两棵线段树,然后在建$n$个点,然后把第一棵线段树上每一个点$(p, l, r)$(结点编号为$p$, 表示区间是$[l, r]$),连边$\for ...

  5. CF786B Legacy(线段树优化建图)

    传送门 ovo这题该怎么做呢?我们首先考虑暴力建图,但是因为建图的操作太多直接就会MLE,所以这个就别想了-- 我们考虑如何优化建图.因为发现一个点可以向一个区间连边,一个区间也可以向一个点连边,想到 ...

  6. CF786B Legacy 线段树优化建图

    洛谷题目链接 题意 首先想到单源最短路,但是如果暴力模拟就会导致从区间里每一个点连向另一个点时最坏情况时间复杂度达到O(N*N),显然会TLE.那么看到区间操作,自然会想到处理区间操作的数据结构,这一 ...

  7. CF786B/CF787D Legacy

    题目描述: luogu cf cf 题解: 最短路+线段树优化建图. 考虑本题的边是点->点.段->点和点->段,我们可以建线段树然后拆成入点和出点. 入点:儿子->父亲,边权 ...

  8. 【CF786B】Legacy

    题目大意:初始给定 N 个点,支持三种操作:两点之间连边:一个点与一个连续区间编号的点之间连边:一个连续区间内的点和一个点连边,求执行 N 次操作之后的单源最短路. 题解:学会了线段树优化建图. 发现 ...

  9. luogu P1549 棋盘问题(2) 题解

    luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...

最新文章

  1. 如何利用客户端在CU发博客
  2. echarts前后端交互数据_如何避免前后端在数据交互方面的相爱相杀?
  3. Codeforces Round #212 (Div. 2) C. Insertion Sort 思维
  4. SpringBoot + AOP 统一处理日志
  5. python捕捉warning_python – 如何格式化logging.captureWarnings捕获的警告?
  6. mongodb服务安装及部署配置
  7. [转] openssl dgst命令完成SHA256哈希校验和RSA数字签名
  8. boost python错误_Ubuntu-链接boost.python-致命错误:找不到pyconfig
  9. Hive高级查询(group by、 order by、 join等)
  10. 计算机网络实验报告3-tcp,计算机网络实验报告3 TCP
  11. Cesium 三角测量(水平距离,直线距离,高度差)
  12. 【#1】小甲鱼新版python学习笔记
  13. 打开qq相册回收站一直显示服务器忙,qq照片回收站怎么打不开 手机qq回收站进不去怎么办...
  14. 解决 Please use the NLTK Downloader to obtain the resource
  15. 计算机自定义桌面设置在哪里设置,如何在windows10桌面设置自定义图片?查看方法...
  16. 讲给后台程序员看的前端系列教程(11)——HTML综合练习
  17. 小Q得到一个神奇的数列: 1, 12, 123,...12345678910,1234567891011...。
  18. (附源码)springboot大学医学生毕业实习分配系统 毕业设计212 002
  19. sklearn——转换器(Transformer)与预估器(estimator)
  20. 计算机科学与技术行业发展历史,发展历程

热门文章

  1. linux云计算架构师:Rsync+sersync实现数据实时同步
  2. 语雀绘制UML时序图
  3. 这一次,让创意成为小游戏的品牌 | 微信小游戏推出四大创意鼓励措施
  4. 搞科研论文看不懂咋办?
  5. java计算机毕业设计书香校园阅读平台源码+系统+mysql数据库+lw文档
  6. 【解决方案】互联网直播系统RTMP推流网关平台EasyRTMPlive在幼儿园家长直播中的应用
  7. 垃圾领导把我的功劳全抢走了!
  8. 揭开网上传销神秘面纱
  9. php switch 函数,详解PHP中switch的使用
  10. MySQL 之 删除语句失败