CodeForces 787D--Legacy(最短路线段树优化建图)
时间:2020-07-10
本文章向大家介绍CodeForces 787D--Legacy(最短路线段树优化建图),主要包括CodeForces 787D--Legacy(最短路线段树优化建图)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
题目链接:https://codeforces.com/contest/787/problem/D
题目大意:有n个星球,你有q个单向传送门
类型1,$u$ $v$ $w$,表示从$u$到$v$花费$w$
类型2,$u$ $l$ $r$ $w$,表示从$u$到区间$[l,r]$中的任意的星球,花费$w$
类型3,$u$ $l$ $r$ $w$,表示从$[l,r]$中任意的星球到$u$,花费$w$,
问从s开始到每个星球的最短距离,如果无法到达则输出-1
Examples
Input
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
Output
0 28 12
Input
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
Output
0 -1 -1 12
emmm,之前已经写过了一篇了,现在在做又WA了好几发QAQ。。。以下是以前写的博客,里面有详细的解说
https://blog.csdn.net/qq_43906000/article/details/102256830
主要就是用两颗线段树来对区间建立路径做个优化
贴一下代码吧QAQ
以下是AC代码:
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define lc rt<<1 #define rc rt<<1|1 typedef long long ll; const int mac=1e5+10; const ll inf=1e18+10; struct node { int id; ll s; bool operator<(const node &a)const { return s>a.s; } }; struct Edge { int to,next,w; }eg[mac*20]; ll dis[mac*10]; int head[mac*10],tree1[mac<<2],tree2[mac<<2]; int idx,pa[mac],pb[mac],num; bool vis[mac*10]; void add(int u,int v,int w) { eg[num]=Edge{v,head[u],w}; head[u]=num++; } void build1(int l,int r,int rt) { tree1[rt]=++idx; if (l==r){ pa[l]=tree1[rt]; return; } int mid=(l+r)>>1; build1(lson);build1(rson); add(tree1[rt],tree1[lc],0); add(tree1[rt],tree1[rc],0); } void build2(int l,int r,int rt) { tree2[rt]=++idx; if (l==r){ pb[l]=tree2[rt]; add(pa[l],tree2[rt],0); return; } int mid=(l+r)>>1; build2(lson);build2(rson); add(tree2[lc],tree2[rt],0); add(tree2[rc],tree2[rt],0); } void update_to_range(int l,int r,int rt,int st,int L,int R,int w) { if (l>=L && r<=R){ add(pb[st],tree1[rt],w); return; } int mid=(l+r)>>1; if (mid>=L) update_to_range(lson,st,L,R,w); if (mid<R) update_to_range(rson,st,L,R,w); } void update_to_pos(int l,int r,int rt,int L,int R,int ed,int w) { if (l>=L && r<=R){ add(tree2[rt],pa[ed],w); return; } int mid=(l+r)>>1; if (mid>=L) update_to_pos(lson,L,R,ed,w); if (mid<R) update_to_pos(rson,L,R,ed,w); } void dij(int st) { priority_queue<node>q; q.push(node{st,0}); dis[st]=0; while (!q.empty()){ node now=q.top(); q.pop(); int u=now.id; if (vis[u]) continue; vis[u]=true; for (int i=head[u]; i!=-1; i=eg[i].next){ int v=eg[i].to; if (vis[v]) continue; if (dis[v]>dis[u]+1LL*eg[i].w){ dis[v]=dis[u]+1LL*eg[i].w; q.push(node{v,dis[v]}); } } } } int main(int argc, char const *argv[]) { //freopen("in.txt","r",stdin); int n,q,s; scanf ("%d%d%d",&n,&q,&s); memset(head,-1,sizeof head); build1(1,n,1); build2(1,n,1); for (int i=1; i<=q; i++){ int id,u,v,w,l,r; scanf ("%d",&id); if (id==1) { scanf ("%d%d%d",&u,&v,&w); add(pb[u],pa[v],w); } else{ scanf ("%d%d%d%d",&v,&l,&r,&w); if (id==2) update_to_range(1,n,1,v,l,r,w); else update_to_pos(1,n,1,l,r,v,w); } } for (int i=1; i<n*10; i++) dis[i]=inf; dij(pb[s]); for (int i=1; i<=n; i++){ if (dis[pb[i]]==inf) printf("%d%c",-1,i==n?'\n':' '); else printf("%lld%c",dis[pb[i]],i==n?'\n':' '); } return 0; }
原文地址:https://www.cnblogs.com/lonely-wind-/p/13281124.html
- 如何在分布式环境中同步solr索引库和缓存信息
- 如何在分布式环境中同步solr索引库和缓存信息
- Info模式下的隐形杀手(SpringMVC同时使用<mvc:resources.../>和FormattingConversionServiceFactoryBean时出现的问题)
- 关于web.xml3.0启动报错
- [机器学习]-[数据预处理]-中心化 缩放 KNN(一)
- 信息安全不可或缺应用交付 还需安全交付
- 一张图带你看懂区块链项目生态
- python环境的安装
- python爬虫抓取网易云音乐歌词
- 区块链学堂——“遇见”拜占庭将军
- NVIDIA把消费级显卡拒绝在数据中心门外
- 保护好手中的比特币,比特币诈骗又见新花样!
- 传万达网络科技集团将大幅减员90% 业务收缩恐为主因
- 企业技术展望:黑科技解放“黄金”劳动力
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 有些包卸载了就回不去了
- 使用 Vue-CLI 3.x 快速搭建「Vue + TS + Kbone + Kbone-UI + 云开发」 项目
- 「R」R Docker 教程(续)
- 为什么R4.0版本内置的R包那么多
- 为何cytoscape总是说我没有java呢
- C#委托和事件
- JSONP原理及使用
- js查询数组或者List类型是否包含某个元素
- jqgrid 获取当前页数据
- C#字符串截取
- 使用Topshelf部署Windows服务
- IIS进程回收后第一次访问慢的问题
- k8s中服务添加hosts及一键转换脚本
- 【一起学系列】之状态模式:你听过“流程”模式吗?
- 【一起学系列】之代理模式:是为了控制访问啊!