题意:

有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\)

路有三种类型:

  • 从点\(v\)到点\(u\)需要花费\(w\)
  • 从点\(v\)到区间\([l,r]\)中的点花费为\(w\)
  • 从区间\([l,r]\)中的点到点\(v\)花费为\(w\)

求起点到各个点的最少花费

分析:

如下图,构建两颗线段树,边的花费都为\(0\)

对于第一种路直接加边即可

对于第二种路,添加从\(v\)到上面线段树对应区间中的点的边

对于第三种路,添加从下面线段树对应区间中的点到\(v\)的边



最后直接跑最短路

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
#define SZ(a) ((int)a.size())
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<LL, int> PII; const int maxn = 100000 + 10; int n, q, s; struct Edge {
int v, w, nxt;
Edge() {}
Edge(int v, int w, int nxt): v(v), w(w), nxt(nxt) {}
}; int ecnt, head[maxn * 9];
Edge edges[maxn * 36]; void AddEdge(int u, int v, int w) {
printf("%d->%d, w = %d\n", u, v, w);
edges[ecnt] = Edge(v, w, head[u]);
head[u] = ecnt++;
} int tot, T[2][maxn << 2];
void build(int t, int o, int L, int R) {
T[t][o] = ++tot;
if(L == R) {
if(0 == t) AddEdge(T[t][o], L, 0);
else AddEdge(L, T[t][o], 0);
return;
}
int M = (L + R) / 2;
build(t, o<<1, L, M);
build(t, o<<1|1, M+1, R);
if(0 == t) {
AddEdge(T[t][o], T[t][o<<1], 0);
AddEdge(T[t][o], T[t][o<<1|1], 0);
} else {
AddEdge(T[t][o<<1], T[t][o], 0);
AddEdge(T[t][o<<1|1], T[t][o], 0);
}
} int type, v, qL, qR, w; void update(int o, int L, int R) {
if(qL <= L && R <= qR) {
if(0 == type) AddEdge(v, T[type][o], w);
else AddEdge(T[type][o], v, w);
return;
}
int M = (L + R) / 2;
if(qL <= M) update(o<<1, L, M);
if(qR > M) update(o<<1|1, M+1, R);
} const LL INF = 0x3f3f3f3f3f3f3f3f;
bool vis[maxn * 9];
LL d[maxn * 9]; priority_queue<PII, vector<PII>, greater<PII> > Q;
void dijkstra() {
memset(d, 0x3f, sizeof(d));
d[s] = 0;
Q.emplace(0, s);
while(!Q.empty()) {
PII t = Q.top(); Q.pop();
int& u = t.second;
if(vis[u]) continue;
vis[u] = true;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int& v = edges[i].v;
int& w = edges[i].w;
if(d[v] > d[u] + w) {
d[v] = d[u] + w;
Q.emplace(d[v], v);
}
}
}
} int main() {
scanf("%d%d%d", &n, &q, &s);
memset(head, -1, sizeof(head));
tot = n;
REP(t, 0, 2) build(t, 1, 1, n); while(q--) {
scanf("%d%d%d", &type, &v, &qL);
if(type == 1) {
scanf("%d", &w);
AddEdge(v, qL, w);
} else {
type -= 2;
scanf("%d%d", &qR, &w);
update(1, 1, n);
}
} dijkstra();
REP(i, 1, n + 1) printf("%lld ", d[i] == INF ? -1 : d[i]);
printf("\n"); return 0;
}

Codeforces 787D Legacy 线段树 最短路的更多相关文章

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

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

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

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

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

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

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

  5. Codeforces 786B. Legacy 线段树+spfa

    题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...

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

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

  7. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  8. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  9. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

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

随机推荐

  1. [topcoder]TheGridDivTwo

    http://community.topcoder.com/stat?c=problem_statement&pm=13628&rd=16278 标程是BFS,我用DFS,都可解. 这 ...

  2. April 9 2017 Week 15 Sunday

    In the evening one may praise the day. 入夜方能赞美白昼. I think that could be understand in different ways, ...

  3. April 7 2017 Week 14 Friday

    A good heart is better than all the brains in the world. 聪明绝顶,不如宅心仁厚. A good heart can be useful to ...

  4. 实现pdf word在线浏览和下载

    这篇实现的是在线展示pdf和word并且不能显示下载和打印按钮 一 下载功能: 因为html5给a标签新添加了一个属性download,这个属性可以直接实现下载文件的功能:<a href=&qu ...

  5. 你真的会用ABAP, Java和JavaScript里的constructor么?

    如果constructor里调用了一个成员方法,这个方法被子类override了,当初始化一个子类实例时,父类的构造函数被的调用,此时父类构造函数的上下文里调用的成员方法,是父类的实现还是子类的实现? ...

  6. Jmeter入门7 测试中使用到的几个定时器和逻辑控制器

    1 测试中提交数据有延时1min,所以查询数据是否提交成功要设置定时器. 固定定时器页面:单位是毫秒 [dinghanhua] 2 集合点.Synchronizing Timer 集合点编辑:集合用户 ...

  7. 页面dom事件防止失效的一种写法

    经常我们写事件是这样的 $('.day').on('click', function () { some(); }); 但是这样在spa模式下经常容易出现页面切换后事件失效的问题, 可以采用这样的写法 ...

  8. .net core 2.0下的容器注册方法

    https://www.cnblogs.com/Wddpct/p/5764511.html 自带的容器注册方法真的很好用

  9. scrapy Pipeline 练习

    class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...

  10. Windows核心编程-作业

    原文链接:http://zhujiangtao.com/?p=983 作业 作业 一个简单例程 CreateJobObject 创建作业 作业限制和 SetInformationJobObject A ...