Codeforces 786B. Legacy 线段树+spfa
题目大意:
给定一个\(n\)的点的图。求\(s\)到所有点的最短路
边的给定方式有三种:
- \(u \to v\)
- \(u \to [l,r]\)
- \([l,r] \to v\)
设\(q\)为给定边的次数,有\(n,q \leq 10^5\)
题解
类比于线段树优化网络流建图
写一个线段树优化最短路建图即可。
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 100010;
struct Edge{
int to,dis;
Edge(){}
Edge(const int &a,const int &b){
to = a;dis = b;
}
};
vector<Edge>ve[maxn*10];
inline void add(int u,int v,int d){
ve[u].push_back(Edge(v,d));
}
int T1[maxn<<2],T2[maxn<<2],nodecnt;
void build1(int rt,int l,int r){
T1[rt] = ++ nodecnt;
if(l == r){
add(l,T1[rt],0);
return ;
}
int mid = l+r >> 1;
build1(rt<<1,l,mid);
build1(rt<<1|1,mid+1,r);
add(T1[rt<<1],T1[rt],0);
add(T1[rt<<1|1],T1[rt],0);
}
void build2(int rt,int l,int r){
T2[rt] = ++ nodecnt;
if(l == r){
add(T2[rt],l,0);
return ;
}
int mid = l+r >> 1;
build2(rt<<1,l,mid);
build2(rt<<1|1,mid+1,r);
add(T2[rt],T2[rt<<1],0);
add(T2[rt],T2[rt<<1|1],0);
}
int p,val;
void query1(int rt,int l,int r,int L,int R){
if(L <= l && r <= R){
add(T1[rt],p,val);
return ;
}
int mid = l+r >> 1;
if(L <= mid) query1(rt<<1,l,mid,L,R);
if(R > mid) query1(rt<<1|1,mid+1,r,L,R);
}
void query2(int rt,int l,int r,int L,int R){
if(L <= l && r <= R){
add(p,T2[rt],val);
return ;
}
int mid = l+r >> 1;
if(L <= mid) query2(rt<<1,l,mid,L,R);
if(R > mid) query2(rt<<1|1,mid+1,r,L,R);
}
ll dis[maxn*10];bool inq[maxn*10];
void spfa(int s){
memset(dis,0x3f,sizeof dis);
dis[s] = 0;inq[s] = true;
queue<int>q;q.push(s);
while(!q.empty()){
int u = q.front();q.pop();
for(vector<Edge>::iterator it = ve[u].begin();it != ve[u].end();++ it){
if(dis[it->to] > dis[u] + it->dis){
dis[it->to] = dis[u] + it->dis;
if(!inq[it->to]){
inq[it->to] = true;
q.push(it->to);
}
}
}inq[u] = false;
}
}
int main(){
int n,q,s;read(n);read(q);read(s);
int t,u,v,l,r;
nodecnt = n;
build1(1,1,n);build2(1,1,n);
while(q--){
read(t);
if(t == 1){
read(u);read(v);read(val);
add(u,v,val);
}else if(t == 2){
read(p);read(l);read(r);read(val);
query2(1,1,n,l,r);
}else if(t == 3){
read(p);read(l);read(r);read(val);
query1(1,1,n,l,r);
}
}
spfa(s);
rep(i,1,n){
if(dis[i] != dis[0]) printf("%lld",dis[i]);
else printf("%d",-1);
if(i != n) putchar(' ');
else putchar('\n');
}
return 0;
}
Codeforces 786B. Legacy 线段树+spfa的更多相关文章
- Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...
- CodeForces - 786B Legacy (线段树+DIjkstra+思维)
题意:给N个点和Q条选项,有三种类型的选项:1.从u到v花费w修建一条路:2.从u到下标区间为[L,R]的点花费w修建一条路; 3.从下标区间为[L,R]的点到u花费w修建一条路. 然后求起点s到其余 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- codeforces 787D - Legacy 线段树优化建图,最短路
题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...
- Codeforces 787D Legacy 线段树 最短路
题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...
- 786B - Legacy(线段树 + 最短路)线段树优化建图
题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...
- CodeForces 786B Legacy(线段树优化建图+最短路)
[题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- Codeforces 786B Legacy(线段树优化建图)
题目链接 Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...
- CF786B Legacy 线段树优化建图 + spfa
CodeForces 786B Rick和他的同事们做出了一种新的带放射性的婴儿食品(???根据图片和原文的确如此...),与此同时很多坏人正追赶着他们.因此Rick想在坏人们捉到他之前把他的遗产留给 ...
随机推荐
- ionic资源网站
http://ionichina.com/topic/570b1f4ecd63e4247a7cfcf3 http://doc.ionicmaterialdesign.com/#intro http:/ ...
- linux练习命令
任务一:按要求完成以下操作1)显示日期格式2)在/tmp/下新建目录test ,并指定权限6643)显示环境变量path,但将/root加入到$PATH中4)用cat显示/etc/passwd,并打印 ...
- pyhton3 logging模块
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message')logging.info('This is info mess ...
- Oracle表约束
约束的概述: 约束是在表中定义的用于维护数据库完整性的一些规则 (1).主键约束 不能为空也不能重复 在一个表中只能定义一个主键约束 Oracle会在主键上建立一个唯一索引,可以指定唯一索引的存储位置 ...
- Linux用户和用户组管理 用户配置和管理的相关文件
用户信息文件 /etc/passwd 这个文件中保存的就是系统中所有的用户及其对应的用户主要信息. 文件格式 : 第1字段 第2字段 第3字段 第4字段 第5字段 第6字段 第7字段 用户名称 密码 ...
- 计算机网络概述 传输层 TCP拥塞控制
TCP拥塞控制 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就 ...
- 【HackerRank】 Filling Jars
Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operatio ...
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- 蓝牙固件升级(OTA升级)原理设计
转:http://blog.csdn.net/yueqian_scut/article/details/50849033 固件空中升级(OTA)与固件二次引导的原理和设计 原创 2016年03月10日 ...
- 什么是JavaBeans?
参看维基百科,归纳出以下几条: JavaBeans是指符合某些标准的类, Bean这个名称用于涵盖这个标准, 其目的在于创建可重用的Java组件. 由于Bean是很“死板”的东西,因此它可以持久存储, ...