CF786B Legacy
思路
线段树优化建图
基本思想就是要把一个区间连边拆成log个节点连边,
然后一颗入线段树,一颗出线段树,出线段树都由子节点向父节点连边(可以从子区间出发),入线段树从父节点向子节点连边(可以到达子区间),入线段树上每个节点向出线段树的每个对应节点连边(进来之后可以出去),题目里的边由出线段树连向入线段树
然后最短路就好了
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define int long long
using namespace std;
int id1[800100*4],id2[800100*4],fir[800100*4],nxt[800100*4],v[800100*4],w[800100*4],lson[800100*4],rson[800100*4],root1,root2,Nodecnt,cnt,n,m,s;
void addedge(int ui,int vi,int wi){
++cnt;
v[cnt]=vi;
w[cnt]=wi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void build(int l,int r,int &o1,int &o2){
o1=++Nodecnt;
o2=++Nodecnt;
addedge(o2,o1,0);
if(l==r){
id1[l]=o1;
id2[l]=o2;
return;
}
int mid=(l+r)>>1;
build(l,mid,lson[o1],lson[o2]);
build(mid+1,r,rson[o1],rson[o2]);
addedge(lson[o1],o1,0);
addedge(rson[o1],o1,0);
addedge(o2,lson[o2],0);
addedge(o2,rson[o2],0);
}
void link(int L,int R,int l,int r,int o,int opt,int v,int w){//0:[l,r]->o 1:o->[l,r]
if(L<=l&&r<=R){
if(!opt)
addedge(o,v,w);
else
addedge(v,o,w);
return;
}
int mid=(l+r)>>1;
if(L<=mid)
link(L,R,l,mid,lson[o],opt,v,w);
if(R>mid)
link(L,R,mid+1,r,rson[o],opt,v,w);
}
struct QNode{
int p,val;
bool operator < (const QNode &b) const{
return val>b.val;
}
};
priority_queue<QNode> q;
int dis[800100*4],vis[800100*4];
void dijkstra(int s){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s]=0;
q.push((QNode){s,0});
while(!q.empty()){
QNode x=q.top();
q.pop();
if(vis[x.p])
continue;
vis[x.p]=true;
for(int i=fir[x.p];i;i=nxt[i]){
if(dis[v[i]]>dis[x.p]+w[i]){
dis[v[i]]=dis[x.p]+w[i];
q.push((QNode){v[i],dis[v[i]]});
}
}
}
}
signed main(){
scanf("%lld %lld %lld",&n,&m,&s);
build(1,n,root1,root2);
for(int i=1;i<=m;i++){
int opt;
scanf("%lld",&opt);
if(opt==1){
int u,v,w;
scanf("%lld %lld %lld",&u,&v,&w);
addedge(id1[u],id2[v],w);
}
else if(opt==2){
int u,l,r,w;
scanf("%lld %lld %lld %lld",&u,&l,&r,&w);
link(l,r,1,n,root2,1,id1[u],w);
}
else if(opt==3){
int l,r,v,w;
scanf("%lld %lld %lld %lld",&v,&l,&r,&w);
link(l,r,1,n,root1,0,id2[v],w);
}
}
dijkstra(id2[s]);
for(int i=1;i<=n;i++)
printf("%lld ",dis[id1[i]]==0x3f3f3f3f3f3f3f3fLL?-1:dis[id1[i]]);
return 0;
}
CF786B Legacy的更多相关文章
- CF786B Legacy && 线段树优化连边
线段树优化连边 要求点 \(x\) 向区间 \([L, R]\) 连边, 一次的复杂度上限为 \(O(n)\) 然后弄成线段树的结构 先父子连边边权为 \(0\) 这样连边就只需要连父亲就可以等效于连 ...
- CF786B Legacy(线段树优化建边)
模板题CF786B Legacy 先说算法 如果需要有n个点需要建图 给m个需要建边的信息,从单点(或区间内所有点)向一区间所有点连边 如果暴力建图复杂度\(mn^2\) 以单点连向区间为例,在n个点 ...
- CF786B Legacy 线段树优化建图
问题描述 CF786B LG-CF786B 题解 线段树优化建图 线段树的一个区间结点代表 \([l,r]\) 区间点. 然后建立区间点的时候就在线段树上建边,有效减少点的个数,从而提高时空效率. 优 ...
- 线段树优化建图 || CF786B Legacy
题面:786B - Legacy 代码: #include<cstdio> #include<cstring> #include<iostream> #includ ...
- CF786B Legacy(线段树优化建图)
嘟嘟嘟 省选Day1T2不仅考了字符串,还考了线段树优化建图.当时不会,现在赶快学一下. 线段树能优化的图就是像这道题一样,一个点像一个区间的点连边,或一个区间像一个点连边.一个个连就是\(O(n ^ ...
- CF786B Legacy 线段树优化建图 + spfa
CodeForces 786B Rick和他的同事们做出了一种新的带放射性的婴儿食品(???根据图片和原文的确如此...),与此同时很多坏人正追赶着他们.因此Rick想在坏人们捉到他之前把他的遗产留给 ...
- [题解] CF786B Legacy
前言 题目链接 题意 有 \(n\) 个点,\(q\) 次连边,以及起点 \(s\) .连边具体分三种: \(1\) \(v\) \(u\) \(w\) 从 \(v\) 到 \(u\) 连一条边. \ ...
- 【CF786B】Legacy
题目大意:初始给定 N 个点,支持三种操作:两点之间连边:一个点与一个连续区间编号的点之间连边:一个连续区间内的点和一个点连边,求执行 N 次操作之后的单源最短路. 题解:学会了线段树优化建图. 发现 ...
- 题解 CF786B 【Legacy】
本题要求我们支持三种操作: ① 点向点连边. ② 点向区间连边. ③ 区间向点连边. 然后跑最短路得出答案. 考虑使用线段树优化建图. 建两颗线段树,入树和出树,每个节点为一段区间的原节点集合.入树内 ...
随机推荐
- Word图片、表格添加题注
1.首先为图片.表格添加题注: 2.通过交叉引用,如图**和图片.表的编号相关联起来:
- 多线程之Executors基本使用
Executors几种创建方式 https://www.cnblogs.com/yasong/p/9318522.html 线程池数如何设置 https://blog.csdn.net/u013276 ...
- Maven -- 在进行war打包时排除不需要的文件
https://blog.csdn.net/zsg88/article/details/78128603 <excludes> <!-- 排除文件,不包含子目录,对WEB-INF目录 ...
- jackson 流式API
http://www.cnblogs.com/lee0oo0/articles/2652528.html Jackson提供了三种可选的JSON处理方法 1.流式API com.fasterx ...
- python类与对象-如何创建可管理的对象属性
如何创建可管理的对象属性 问题举例 在面向对象编程中, 我们把方法看作对象的接口, 直接访问对象的属性可能是不安全的,或设计上不够灵活. 但是使用调用方法在形式上不如访问属性简洁. circle.ge ...
- Leetcode: Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 自动化运维工具——ansile详解
自动化运维工具——ansible详解(一) 目录 ansible 简介 ansible 是什么? ansible 特点 ansible 架构图 ansible 任务执行 ansible 任务执行模式 ...
- C# string 常用功能的方法扩展
#region Usings using System; using System.Text; using System.Data; using System.Data.SqlClient; usin ...
- Python生成器的原理及使用
'''1,什么是生成器? 函数内但凡有一个yield关键字, 再调用函数就不会执行函数代码,得到的返回值就是一个生成器对象 生成器本身就是一种迭代器 next(g)过程: 会触发生成器g所对应的函数的 ...
- org.w3c.dom.Node.getTextContent()方法编译错误-已解决
org.w3c.dom.Node.getTextContent()方法编译错误. 在项目的Java Build Path | Order and Export选项卡中,将JRE System Libr ...