codeforces CF786B CF787D Legacy 线段树优化建图

$ \Rightarrow $ 戳我进CF原题

D. Legacy

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

 

Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them.
So Rick wants to give his legacy to Morty before bad guys catch them.
 
There are $ n $ planets in their universe numbered from $ 1 $ to $ n $ .
Rick is in planet number $ s $ (the earth) and he doesn't know where Morty is.
As we all know, Rick owns a portal gun.
With this gun he can open one-way portal from a planet he is in to any other planet (including that planet).
But there are limits on this gun because he's still using its free trial.

ba1b063d9f3f1edc4550fe3a21159354e74734c9.png

By default he can not open any portal by this gun.
There are $ q $ plans in the website that sells these guns.
Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
 
Plans on the website have three types:
 
1.With a plan of this type you can open a portal from planet $ v $ to planet $ u $.
2.With a plan of this type you can open a portal from planet $ v $ to any planet with index in range $ [l, r] $.
3.With a plan of this type you can open a portal from any planet with index in range $ [l, r] $ to planet $ v $.
 
Rick doesn't known where Morty is,
but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately.
So for each planet (including earth itself) he wants to know
the minimum amount of money he needs to get from earth to that planet.
 

Input

The first line of input contains three integers $ n, q $ and $ s (1 ≤ n, q ≤ 10^5, 1 ≤ s ≤ n) $
— number of planets, number of plans and index of earth respectively.
 
The next $ q $ lines contain the plans.
Each line starts with a number $ t $ , type of that plan $ (1 ≤ t ≤ 3) $ .
If $ t = 1 $ then it is followed by three integers $ v, u $ and $ w $ where $ w $ is the cost of that plan $ (1 ≤ v, u ≤ n, 1 ≤ w ≤ 10^9) $ .
Otherwise it is followed by four integers $ v, l, r $ and $ w $ where $ w $ is the cost of that plan $ (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 10^9) $ .
 

Output

In the first and only line of output print $ n $ integers separated by spaces.
$ i $-th of them should be minimum money to get from earth to $ i $-th planet, or $  -1 $ if it's impossible to get to that planet.
 

Examples

input1
 3 5 1
 2 3 2 3 17
 2 3 2 2 16
 2 2 2 3 3
 3 3 1 1 12
 1 3 3 17
output1
 0 28 12

input2
 4 3 1
 3 4 1 3 12
 2 2 3 4 10
 1 2 4 16
output2
 0 -1 -1 12

 

Note

In the first sample testcase, Rick can purchase $ 4 $ th plan once
and then $ 2 $ nd plan in order to get to get to planet number $ 2 $ .
 

题目大意

  • $ n(n \le 10^5) $ 个点构成的有向图,有 $ m(m \le 10^5) $ 条连通信息,信息有三种:

  • $ 1 \quad u \quad v \quad w $ ,表示存在一条边权为 $ w $ 的有向边 $ (u,v)) $ ;
  • $ 2 \quad u \quad L \quad R \quad w $ ,表示 $ \forall v \in \quad [L,R] $ ,存在一条边权为 $ w $ 的有向边 $ (u,v) $ ;
  • $ 3 \quad u \quad L \quad R \quad w $ ,表示 $ \forall v in \quad [L,R] $ ,存在一条边权为 $ w $ 的有向边 $ (u,v) $ 。

  • 其中 $ w \le 10^9 $ 。求点 $ s $ 到每个点的最短路,不存在输出 $ -1 $。
     

思路

  • 线段树优化建图。

  • 建立两棵线段树,其上点的点权分别表示“到达这个区间内所有点的最小花费”和“到达这个区间内任意一个点的最小花费”。

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

  • 对于第二种路,添加从 $ v $ 到第一棵线段树对应区间中的点的边

  • 对于第三种路,添加从从第二棵线段树对应区间中的点到 $ v $ 的边
     

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define N 100005
#define M 300005
#define ll long long
using namespace std;
int n,m,s,cnt,rt1,rt2;
int head[M],ls[M],rs[M],tot;
struct edge{ int v,w,nxt; }e[N * 20];
inline void add(int u,int v,int w) { 
    e[++tot].v=v; e[tot].w=w; e[tot].nxt=head[u]; head[u]=tot;
}
void build1(int &o,int l,int r) { 
    if(l==r){ o=l; return; }
    o=++cnt; 
    int mid=l+r>>1;
    build1(ls[o],l,mid); build1(rs[o],mid+1,r);
    add(o,ls[o],0);      add(o,rs[o], 0); 
}
void build2(int &o,int l,int r) { 
    if(l==r){ o=l; return; }
    o=++cnt; 
    int mid=l+r>>1;
    build2(ls[o],l,mid); build2(rs[o],mid+1,r);
    add(ls[o],o,0);      add(rs[o],o, 0); 
}
int L,R;
void updata(int o,int l,int r,int u,int w,int p) {
    if(L<=l&&r<=R) { 
        p==2 ? add(u, o, w) : add(o, u, w);
        return;
    }
    int mid=l+r>>1;
    if(L<=mid) updata(ls[o],l,mid,u,w,p);
    if(mid<R) updata(rs[o],mid+1,r,u,w,p);
}
const ll INF = 0x3F3F3F3F3F3F3F3F;
ll dis[M];
queue<int> Q;
bool vis[M];
void SPFA(int s) {  
    memset(dis,0x3F,sizeof(dis));
    dis[s] = 0; Q.push(s); vis[s]=1;
    while(!Q.empty()) {
        int u=Q.front(); Q.pop(); vis[u]=0;
        for(int i=head[u];i;i=e[i].nxt)
            if(dis[u]+e[i].w<dis[e[i].v]){
                dis[e[i].v]=dis[u]+e[i].w;
                if(!vis[e[i].v]){ vis[e[i].v]=1; Q.push(e[i].v); }
            }
    }
}
int main() {
    scanf("%d %d %d",&n,&m,&s);
    cnt = n;
    build1(rt1, 1, n); build2(rt2, 1, n);
    while(m--){
        int opt,u,v,w;
        scanf("%d",&opt);
        if(opt==1){
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w); 
        } else {
            scanf("%d %d %d %d",&u,&L,&R,&w);
            updata(opt == 2 ? rt1 : rt2,1,n,u,w,opt);
        }
    }
    SPFA(s);
    for(int i = 1; i <= n; i++) 
        printf("%lld ",dis[i]<INF ? dis[i] : -1);
    return 0;
}

转载于:https://www.cnblogs.com/PotremZ/p/CF786B.html

Arduino--土壤湿度传感器使用(电阻式) 最近买了一个土壤湿度检测传感器,实物是这样的: 我买这个模块是想要得到土壤湿度的模拟量。但我买的时候忘了买左边的蓝色模块,我开始以为这个模块是有运放功能的,但实际它是一个电压比较器和一个分压电路。 其模块的接线如: 该模块的四线是带模拟量输出的,三线的只输出一个到达阈值的标志量。该模块的原理如下: 该模块的测试原理为:把土壤当成一个可变电阻,随着土壤含水量的增加,土壤电阻减小。而该传感器其实就是两个电极,用来接土壤这个可变电阻。一般湿度的土壤电阻大概在几百K左右,其串连的分压电阻应该在这个数量级。 阅读详情

相关推荐

Halcon 20.11在WPF中的交互式绘:完整实现矩形ROI的创、拖动与坐标获取

本文详细介绍了如何在WPF框架下利用Halcon 20.11实现交互式矩形ROI的创、拖动与坐标获取。通过HDrawingObject和HSmartWindowControlWPF控件,开发者可以高效构机器视觉应用,提升项目实战中的开发效率。文章涵盖环境配置、事件处理、坐标获取及性能优化等关键内容。

weixin_29266007的博客 395

CF 787D Legacy线段树思想构+最短路)

D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Rick and his co-workers have made a new radioactive formu...

weixin_30389003的博客 309

ZXR10 5250系列交换机用户手册.pdf

中兴ZXR10 5250系列交换机配置手册,资料版本 R1.0,产品版本V1.0,全千兆智能交换机配置手册。

CF787D】遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)

Problem遗产 题目大意 给出一个带权有向,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 求st点到每个点的最短路 Solution 首先我们思考到,若是每次对于l,r区间内的每一个点都执行一次加边操作,不仅耗时还耗空间。 那么我们要想到一个办法去优化它。一看...

dichengting7485的博客 1770

CF 787D 线段树套堆优化Dij

题目链接:http://codeforces.com/contest/786/problem/B 题意:就是给了n个点,给了m个加边的关系,有向边,但是加边不一定是u->v,可能是u->[l, r]就是u到[l, r]区间里面的每一个点都加一条边,或者是[l, r] -> u,就是[l, r]区间到u加一条边。然后求单源最短路。解法:显然单源最短路用堆优化的DIJ来求。先考虑一下,如果我们把区间拆

I good vegetable a! 1429

cf786D(Legacy 线段树优化跑最短路 模板)

题目 #include<bits/stdc++.h> #define m(a,b) memset(a,b,sizeof a) #define fnq freopen("input.txt","r",stdin) using namespace std; typedef long long ll; const int N=1e6+5; const ll INF=1e17; struct...

qq_42576687的博客 330

2018.09.16 周总结

话说我周一就开始写周总结是不是有点早啊ˋ( ° ▽、° ) Re:不早,你甚至中间几天都没来过,你甚至那时都不知道炸弹的恐怖(゜ω゜) ┑( ̄Д  ̄)┍如何学会不生气阶段经历中~~~ 周出题解 1.codeforces CF85E Guard Towers 二分答案 二分判定 2.codeforces CF786B CF787D Legacy 线段树优化 3.codef...

ayf1988的博客 174

CF786-D ABC-SORT

Problem - D - Codeforces 贪心。 从A的末端插到B的中间,再从B的中间插到C的末端。 可以证明,A->B的过程中,插到中间的左边,和右边的结果是一样的 又因为A->B先插入的,B->C先取出。B->C若长度为偶数可以从中间两个中取较小的。 所以只考虑末尾的两个数即可,使用vector进行插入和删除操作,由于只对末尾进行操作,故复杂度为O(n) 最后检查是否为non-decreasing即可 代码如下 #define _CRT_SECURE..

the_unrepentant的博客 248

codeforces 787D (线段树+dij)

D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Rick and his co-workers have made a new radioactive formula and a lot o...

yjt9299的博客 929

CF786B Legacy 线段树优化

洛谷题目链接 题意 首先想到单源最短路,但是如果暴力模拟就会导致从区间里每一个点连向另一个点时最坏情况时间复杂度达到O(N*N),显然会TLE。那么看到区间操作,自然会想到处理区间操作的数据结构,这一题就是一道线段树题。 想法 【线段树优化】 1.由于直接暴力模拟连边导致TLE,我们可以按照题意两颗线段树TreeIn(外部一点连向一个区间)和TreeOut(一个区间连向一个点),线...

学习记录 257

CF787D Legacy

codeforces 线段树优化的裸题 代码: #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<queue> using namespace std; #define rg register void rea...

weixin_30670925的博客 194

CodeForces - 786B Legacy线段树 +最短路+思维好题)

Legacy(传送门) 题意 给定nn颗行星,qq次处理,地球位置为ss,求解在qq次处理后,地球到每一颗行星的位置。其中qq有三种不同的操作: 输入v,u,wv,u,w,构一条从vv到uu的代价为ww的路线 输入u,l,r,wu, l, r, w,构一条从uu到区间[l,r][l,r]中任意一颗行星的代价为ww的路线 输入u,l,r,wu, l, r, w,构区间[l,r]中任意一颗行星

qq_18661257的专栏 1894

codeforces 786B (线段树+最短路径)

线段树,跑最短路

Out_of_Cage的博客 699

线段树优化学习

线段树优化 在某次CYjian神犇给我推了一道树剖+线段树优化+2-SAT的好(毒瘤)题后,发现自己竟然还 不会线段树优化,于是赶紧滚去学了一波,记下笔记 作用 用来解决区间连边(l,r)的每个点向(L,R)的每个点连边的方法。可以优化空间和时间。 实现 以CF786B为例。 题目要求支持以下三个操作: 单个点向单个点连边,权值为v [l,r][l,r][l,r]中的...

Galaxy_yr的博客 691

[二分 前缀优化 2-SAT] Codeforces 587D. Duff in Mafia

二分答案对于一个点,最多能删一条相连的边,每一种颜色最多能有一条相连的边,那么把每个点相连的边用前缀优化就可以了#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <cstring>using namespace std;const int N=1000010;int n,m,

CE玩家 653

CodeForces - 786B Legacy (线段树+最短路)

B. Legacy time limit per test 2 seconds memory limit per test256 megabytes inputstandard inputoutputstandard output Rick and his co-workers have made a new radioactive formula and a lot of bad guys...

GYH0730的博客 908

[线段树 & 前缀 优化 二分 2-SAT] CF Gym100159 facebook-hacker-cup-2012 I. Unfriending

二分答案对于一个点,肯定有一个区间里的点和它不能共存这个可以用线段树优化,每个friend list最多只能删一个,用前缀优化还要一些针对随机数据什么的trick才能过…比如二分中的那个特判#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <cstring>using n

CE玩家 728

OpenStack镜像管理避坑指南:从Glance服务验证到镜像格式转换的完整流程

本文深入探讨了OpenStack镜像管理的核心流程,从Glance服务验证到镜像格式转换的完整避坑指南。通过实战技巧和故障排查方法,帮助运维人员高效管理镜像生命周期,解决常见问题如API兼容性、镜像上传异常和格式转换失败等,提升云平台运维效率。

weixin_30402343的博客 400

【2-sat】Gym - 101201F - Illumination

题意:平面上l盏灯,每盏灯可以照亮横向的2*r+1个格子或者纵向的2*r+1个格子,让你确定每盏灯的方向,使得每个格子只被同一行的不超过一盏灯照亮,并且只被同一列的不超过一盏灯照亮。输出是否有解。 显然的2-sat模型。 #include<cstdio> #include<cstring> #include<vector> using namesp...

weixin_34137799的博客 148
上一篇: codeforces CF85E Guard Towers 二分答案 二分图判定
下一篇: Test 2018-09-12
ayf1988
博客等级 码龄12年 0粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值