【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy
题目连接:
http://codeforces.com/contest/786/problem/B
Description
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 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.
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.
Sample Input
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
Sample Output
0 28 12
Hint
题意
三种操作:
1 a b c,在建立权值为c的a->b的单向边
2 a b c d,建立a->[b,c]权值为d的单向边
3 a b c d,建立[b,c]->a权值为d的单向边。
给你一个起点,问你起点到其他点的最短路长度。
题解:
如果暴力建边的话,显然会有n^2个边。
但是我们用线段树去建边就好了,我们依次让所有节点都指向自己区间的l端点和r端点就行了。
我相当于预先又建了nlogn个节点,这些虚拟节点代替区间。
然后跑dij就好了
以下解释来自:http://www.cnblogs.com/GXZlegend/p/7016722.html
一个朴素(已经不是最朴素的了)的加边方法:a~b的所有点->p1,长度为0;p1->p2,长度为1;p2->c~d的所有点,长度为0,其中加的都是有向边,p1和p2是新建的两个辅助点,然后再反过来进行这个过程。
然而这样加边的话边数依旧巨大。
由于给出的加边都是区间形式,所以我们可以用维护区间的数据结构——线段树,去优化这个建图过程。
具体方法(这里只讲加有向边a~b->c~d的方法):
建立两颗线段树A、B,其中A线段树每个非叶子节点的儿子向该节点连边,长度为0,B线段树每个非叶子节点向该节点的儿子连边,长度为0;B线段树的叶子结点向A线段树对应的叶子结点连边,长度为0。
这里面A线段树的叶子结点代表原图中的节点,其余节点都是用来优化建图。
对于加边操作,找到A线段树上a~b对应的区间节点,这些节点向p1连边,长度为0;p1->p2,长度为1;找到B线段树上c~d对应的区间节点,p2向这些节点连边,长度为0.
最后跑堆优化Dijkstra出解。
应该不是很难理解,具体可以见代码。
图片来自:http://blog.csdn.net/weixin_37517391/article/details/77073700
代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int maxn = 2e6+7;
4 vector<pair<int,int> >v[maxn];
5 long long dist[maxn],ver[2][maxn];//ver0表示左边的线段树,1表示右边的线段树
6 int n,q,ss,tme;
7 set<pair<long long,int> >s;
8 int build(int y,int l,int r,int x){
9 if(l==r) return ver[x][y]=l; //注意这个操作,有了这个操作,就将虚设的节点与原先的n个节点连接起来了
10 ver[x][y]=++tme;
11 int mid=(l+r)/2;
12 int cl=build(y*2,l,mid,x);
13 int cr=build(y*2+1,mid+1,r,x);
14 if(x==0){
15 v[ver[x][y]].push_back(make_pair(cl,0));
16 v[ver[x][y]].push_back(make_pair(cr,0));
17 }else{
18 v[cl].push_back(make_pair(ver[x][y],0));
19 v[cr].push_back(make_pair(ver[x][y],0));
20 }
21 return ver[x][y];
22 }
23 void update(int x,int l,int r,int ll,int rr,int xx,int w,int z){
24 if(l>rr||r<ll) return;
25 if(l>=ll&&r<=rr){
26 if(z==0) v[xx].push_back(make_pair(ver[z][x],w));
27 else v[ver[z][x]].push_back(make_pair(xx,w));
28 return;
29 }
30 int mid=(l+r)/2;
31 update(x*2,l,mid,ll,rr,xx,w,z);
32 update(x*2+1,mid+1,r,ll,rr,xx,w,z);
33 }
34 int main(){
35 cin>>n>>q>>ss;
36 memset(dist,-1,sizeof(dist));
37 tme=n;
38 build(1,1,n,0); //建立左边线段树的虚节点
39 build(1,1,n,1); //建立右边线段树的虚节点
40 for(int i=0;i<q;i++){
41 int t,a,b,c,d;
42 cin>>t>>a>>b>>c;
43 if(t==1){
44 v[a].push_back(make_pair(b,c)); //单点直接连边即可
45 }else{
46 cin>>d;
47 update(1,1,n,b,c,a,d,t-2); //update,最后一个参数为flag,只有0或1
48 }
49 }
50 dist[ss]=0;
51 priority_queue<pair<long long,int> >Q;
52 Q.push(make_pair(0,ss));
53 while(!Q.empty()){
54 int now = Q.top().second;
55 Q.pop();
56 for(int i=0;i<v[now].size();i++){
57 int ve=v[now][i].first;
58 int co=v[now][i].second;
59 if(dist[ve]==-1||dist[now]+co<dist[ve]){
60 dist[ve]=dist[now]+co;
61 Q.push(make_pair(-dist[ve],ve));
62 }
63 }
64 }
65 for(int i=1;i<=n;i++)
66 cout<<dist[i]<<" ";
67 cout<<endl;
68 }
【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路的更多相关文章
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #406 (Div. 2) 787-D. Legacy
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
随机推荐
- 移动端APP测试概要
APP测试点总结(全面) 一.功能性测试: ——根据产品需求文档编写测试用例. ——软件设计文档编写用例. 注意:就是根据产品需求文档编写测试用例而进行测试. 二.兼容性测试: ——android版本 ...
- IDEA的常见的设置和优化(功能)
转载 原文:https://blog.csdn.net/zeal9s/article/details/83544074
- 第一讲,DOS头文件格式
今天讲解PE文件格式的DOS头文件格式 首先我们要理解,什么是文件格式,我们常说的EXE可执行程序,就是一个文件格式,那么我们要了解它里面到底存了什么内容 简短的说明. 我们要知道,PE文件格式,是微 ...
- Jmeter4.0---- jmeter逻辑控制器(16)
1.说明 逻辑控制器可以帮助用户控制Jmeter的测试逻辑,特别是何时发送请求.逻辑控制器可以改变其子测试元件的请求执行顺序. 2.逻辑控制器 (1)如果(if)控制器 用法一: 审核人员,数据分为 ...
- 操作系统中堆(heap)与栈(stack)的区别
主要区别如下: 一.空间分配: 1.堆(操作系统):一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收,分配方式类似于链表.PS:java中都是系统GC,程序员无法进行GC. 2.栈(操作 ...
- js之数据类型(对象类型——单体内置对象——Math)
Math是一个内置对象,它具有数学常数和函数的属性和方法.Math对象用于执行数学任务,和其它对象不同,Math只是一个静态对象并没有Math()构造函数,实际上,Math()只是一个由js设置的对象 ...
- 使用百度echarts仿雪球分时图(三)
这章节将完成我们的分时图,并使用真实的数据来进行展示分时图. 一天的交易时间段分为上午的09:30~11:30,下午的13:00~15:00两个时间段,因为分时间段的关系,数据是不连续的,所以会先分为 ...
- asp.net 简单的身份验证
1 通常我们希望已经通过身份验证的才能够登录到网站的后台管理界面,对于asp.net 介绍一种简单的身份验证方式 首先在webconfig文件中添加如下的代码 <!--身份验证--> &l ...
- windows 日志清理和设置
Windows日志路径 c:/windows/system32/winevt/logs 这里的日志文件可以ctrl+a 选中后使用shift+delete进行删除,删不掉的可以点击跳过. 在“管理 ...
- Asp.net Core 微信小程序支付
最近要做一个微信小程序支付的功能 在网上找了一下 .net Core做微信支付的博客 和 demo 几乎没有 自己研究了好几天 参考了 很多 大牛的博客 勉强做出来了 因为参数都没有 比如 opid ...