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想在坏人们捉到他之前把他的遗产留给 ...
随机推荐
- PAT 天梯赛 L1-031. 到底是不是太胖了 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-031 AC代码 #include <iostream> #include <cstdio&g ...
- $python正则表达式系列(2)——re模块常用函数
本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...
- 交叉编译Mesa,X11lib,Qt opengl
记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...
- html5-entities.js消失问题
今天用nuxt做项目时,启动npm run dev,项目正常启动, 可过一会儿再试图启动时却报错: * ./libhtml5-entities.js in ./~/html-entities/inde ...
- Ubuntu: 无法使用su命令
Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...
- QT应用程序设置图标
一.纯Qt 1.下载图标:app.ico 2.新建记事本,输入:IDI_ICON1 ICON DISCARDABLE"app.ico":改变名字为jude.rc 3.将两个文件放在 ...
- 4.JDBC编程
01.JDBC_Java程序和MySQL的关系: 1).Java程序跟其它MySQL客户端一样,就是一个"客户端",用于"封装SQL语句"并发送给MyS ...
- sql server deadlock problem
https://www.red-gate.com/simple-talk/sql/learn-sql-server/how-to-track-down-deadlocks-using-sql-serv ...
- Luogu-1527 [国家集训队]矩阵乘法
Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...
- java创建多线程的三种方式
/***************************继承Thread类创建多线程************************/ public class FirstThread extends ...