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想在坏人们捉到他之前把他的遗产留给 ...
随机推荐
- OpenGL帧缓存对象(FBO:Frame Buffer Object)
http://blog.csdn.net/dreamcs/article/details/7691690 转http://blog.csdn.net/xiajun07061225/article/de ...
- RAID 工作模式
RAID 工作模式 RAID磁盘阵列 优点: 1.提高传输速率:RAID通过在多个磁盘上同时存储和读取数据来大幅提高存储系统的数据吞吐量. 2.RAID可以达到单个磁盘驱动器几倍.几十倍甚至上百倍的速 ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)
系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...
- 20145240 《Java程序设计》第八周学习总结
20145240 <Java程序设计>第八周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外配 ...
- NSFetchedResultController与UITableView
1 #import "AppDelegate.h" #import "Book.h" @interface AppDelegate () @end @imple ...
- [算法]在单链表和双链表中删除倒数第k个结点
题目: 分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点. 要求: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1). 解答: 让链表从头 ...
- 关于图片上传与下载(Java)
图片的上传 package com.upload; import java.io.IOException;import java.io.PrintWriter; import javax.servle ...
- RPC与HTTP
一.为什么需要RPC,而不是简单的HTTP接口? RPC(即Remote Procedure Call,远程过程调用),主要是基于TCP/IP协议:而HTTP服务主要是基于HTTP协议的.我们都知道H ...
- 探究操作系统【TLCL】
ls – List directory contents file – Determine file type less – View file contents ls常用选项 ls -a 全部输出 ...
- Solr 报错:java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody
HBase协处理器中使用Solr时报错,如下 2018-07-11 17:06:14,054 INFO [LruBlockCacheStatsExecutor] hfile.LruBlockCache ...