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就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+7;
vector<pair<int,int> >v[maxn];
long long dist[maxn],ver[2][maxn];
int n,q,ss,tme;
set<pair<long long,int> >s;
int build(int y,int l,int r,int x){
if(l==r)return ver[x][y]=l;
ver[x][y]=++tme;
int mid=(l+r)/2;
int cl=build(y*2,l,mid,x);
int cr=build(y*2+1,mid+1,r,x);
if(x==0){
v[ver[x][y]].push_back(make_pair(cl,0));
v[ver[x][y]].push_back(make_pair(cr,0));
}else{
v[cl].push_back(make_pair(ver[x][y],0));
v[cr].push_back(make_pair(ver[x][y],0));
}
return ver[x][y];
}
void update(int x,int l,int r,int ll,int rr,int xx,int w,int z){
if(l>rr||r<ll)return;
if(l>=ll&&r<=rr){
if(z==0)v[xx].push_back(make_pair(ver[z][x],w));
else v[ver[z][x]].push_back(make_pair(xx,w));
return;
}
int mid=(l+r)/2;
update(x*2,l,mid,ll,rr,xx,w,z);
update(x*2+1,mid+1,r,ll,rr,xx,w,z);
}
int main(){
cin>>n>>q>>ss;
memset(dist,-1,sizeof(dist));
tme=n;
build(1,1,n,0);
build(1,1,n,1);
for(int i=0;i<q;i++){
int t,a,b,c,d;
cin>>t>>a>>b>>c;
if(t==1){
v[a].push_back(make_pair(b,c));
}else{
cin>>d;
update(1,1,n,b,c,a,d,t-2);
}
}
dist[ss]=0;
priority_queue<pair<long long,int> >Q;
Q.push(make_pair(0,ss));
while(!Q.empty()){
int now = Q.top().second;
Q.pop();
for(int i=0;i<v[now].size();i++){
int ve=v[now][i].first;
int co=v[now][i].second;
if(dist[ve]==-1||dist[now]+co<dist[ve]){
dist[ve]=dist[now]+co;
Q.push(make_pair(-dist[ve],ve));
}
}
}
for(int i=1;i<=n;i++)
cout<<dist[i]<<" ";
cout<<endl;
}

Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路的更多相关文章

  1. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  7. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  8. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  9. 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 ...

随机推荐

  1. 深入解析内存原理:RAM的基本原理

    1. 寻址原理概述RAM 主要的作用就是存储代码和数据供CPU 在需要的时候调用.但是这些数据并不是像用袋子盛米那么简单,更像是图书馆中用有格子的书架存放书籍一样,不但要放进去还要能够在需要的时候准确 ...

  2. 虚拟机Ubuntu 18.04安装RabbitMQ 3.7.9

    Windows 10家庭中文版,VirtualBox,Ubuntu 18.04,Rabbitmq 3.7.9,Erlang/OTP 20 [erts-9.2], 在虚拟机上装好了Ubuntu,写了一个 ...

  3. OracleOCP认证 之 Linux基础

    Linux 基础 一.SHELL 1: Shell 简介 shell 是用户和Linux 操作系统之间的接口.Linux 中有多种shell, 其中缺省使用的是bash. Linux 系统的shell ...

  4. LeetCode(63):不同路径 II

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  5. 性能测试十四:Xshell链接linux虚拟机

    一.先装一个linux虚拟机 VBox+centos1.先下载Linux镜像文件的ovf或者OVA文件2.打开vbox,点击菜单栏“管理”-“导入虚拟电脑3.选择解压路径中的ovf或者OVA文件,点击 ...

  6. python 全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)

    一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...

  7. ASP.NET Global.asax详解【转】

    global.asax是一个文本文件,它提供全局可用代码.这些代码包括应用程序的事件处理程序以及会话事件.方法和静态变量.有时该文件也被称为应用程序文件. global.asax 文件中的任何代码都是 ...

  8. xxx is not in sudoers file 解决(转)

    解决方案:首需要切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用& ...

  9. android系统属性获取及设置

    系统属性获取及设置中的设置值 data/data/com.android.providers.settings/databases/settings.db 1.系统属性获取及设置 android.os ...

  10. #9 //[SDOI2017]新生舞会

    题解: 分数规划+费用流 常数巨大开o2加inline加register还是不行 我也不知道为什么 代码: #include <bits/stdc++.h> using namespace ...