题目大意:

给定一个\(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的更多相关文章

  1. Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)

    题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...

  2. CodeForces - 786B Legacy (线段树+DIjkstra+思维)

    题意:给N个点和Q条选项,有三种类型的选项:1.从u到v花费w修建一条路:2.从u到下标区间为[L,R]的点花费w修建一条路; 3.从下标区间为[L,R]的点到u花费w修建一条路. 然后求起点s到其余 ...

  3. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

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

  5. Codeforces 787D Legacy 线段树 最短路

    题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...

  6. 786B - Legacy(线段树 + 最短路)线段树优化建图

    题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...

  7. CodeForces 786B Legacy(线段树优化建图+最短路)

    [题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...

  8. Codeforces 786B Legacy(线段树优化建图)

    题目链接  Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...

  9. CF786B Legacy 线段树优化建图 + spfa

    CodeForces 786B Rick和他的同事们做出了一种新的带放射性的婴儿食品(???根据图片和原文的确如此...),与此同时很多坏人正追赶着他们.因此Rick想在坏人们捉到他之前把他的遗产留给 ...

随机推荐

  1. val() attr('value')

    val() 只能更改输入框内的值,能更改value属性, 在浏览器中体现不出value被改变 attr('value') 都可以 谷歌浏览器 val,attr都能获取输入框最新的value值

  2. c# 接口(interface)与接口应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //接口(interface ...

  3. java的服务端与客户端通信(1)

    一.理解socket 1.1什么是socket? socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络 ...

  4. Shell 条件判断总结

    -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若文件存在,则为真 -f ...

  5. 嵌入式C函数优化

    0. 引言 这是一个简单函数的优化,但却体现了代码易读性和效率的综合考虑. 如果问我如何写出优秀的代码,答曰:再写一版. 1. 版本1 从环形buffer中取出数据,然后放到一个结构体中.buffer ...

  6. 摊铺机基本参数介绍(鼎盛天工WTD9501A)

    柴油水冷发动机,马力强劲,功率储备系数大,低噪音.低污染,经济性好,低温起动性能好.微电子控制,分别实现刮板输送.螺旋供料左右独立驱动,可实现自动供料,保持熨平板前物料均匀,调平系统响应速度快,调平精 ...

  7. 主攻ASP.NET MVC4.0之重生:使用反射获取Controller的ActionResult

    示例代码 public ActionResult TypeOfForName() { Type typeinfo = typeof(CustomerClassController); //typeof ...

  8. bowtie2用法

    bowtie2的功能:短序列的比对 用法:bowtie2 [options]* -x <bt2-idx> {-1 <m1> -2 <m2> | -U <r&g ...

  9. request模块 一基础部分

    一.HTTP请求   通过requests发送网络请求,方法有get post put delete head options import requests r=requests.get(" ...

  10. EXP-00008:遇到ORACLE错误904问题

    案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...