G. 神圣的 F2 连接着我们 线段树优化建图+最短路
这个题目和之前写的一个线段树优化建图是一样的。
B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
之前这个题目可以相当于一个模板,直接套用就可以了。
不过注意为了提高效率,在区间与区间之间建边的时候建了两个虚点。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
int numa[maxn * ], numb[maxn * ], lefta[maxn * ], leftb[maxn * ];
int start[maxn], endss[maxn];
ll d[maxn * ], tot;
int n, m, p, q;
bool vis[maxn * ];
struct edge {
int from, to, dist;
edge(int from = , int to = , int dist = ) :from(from), to(to), dist(dist) {}
};
struct heapnode {
ll d;
int u;
heapnode(ll d = , int u = ) : d(d), u(u) {}
bool operator<(const heapnode &a) const {
return a.d < d;
}
}; vector<edge> vec;
vector<int> g[maxn * ]; void add(int u, int v, int w) {
vec.push_back(edge(u, v, w));
int m = vec.size();
g[u].push_back(m - );
// printf("u=%d v=%d w=%d\n", u, v, w);
} void dijkstra() {
priority_queue<heapnode>que;
for (int i = ; i <= tot; i++) d[i] = inf64;
for(int i=;i<=q;i++)
{
int id = lefta[start[i] + n];
d[id] = ;
que.push(heapnode(, id));
}
memset(vis, , sizeof(vis));
while (!que.empty()) {
heapnode x = que.top(); que.pop();
int u = x.u;
// printf("u=%d\n", u);
if (vis[u]) continue;
vis[u] = ;
for (int i = ; i < g[u].size(); i++) {
edge &e = vec[g[u][i]];
// printf("u=%d e.to=%d e.dist=%d\n", u, e.to, e.dist);
// printf("d[%d]=%lld d[%d]=%lld\n", u, d[u], e.to, d[e.to]);
if (d[e.to] > d[u] + e.dist) {
// printf("ww\n");
d[e.to] = d[u] + e.dist;
// printf("d[%d]=%lld\n", e.to, d[e.to]);
que.push(heapnode(d[e.to], e.to));
}
}
// printf("\n");
}
} void builda(int id, int l, int r) {
numa[id] = ++tot;
int mid = (l + r) >> ;
if (l == r) {
lefta[l] = tot;
return;
}
builda(id << , l, mid);
builda(id << | , mid + , r);
add(numa[id << ], numa[id], );
add(numa[id << | ], numa[id], );
} void buildb(int id, int l, int r) {
numb[id] = ++tot;
int mid = (l + r) >> ;
if (l == r) {
leftb[l] = tot;
return;
}
buildb(id << , l, mid);
buildb(id << | , mid + , r);
add(numb[id], numb[id << ], );
add(numb[id], numb[id << | ], );
} void build3(int n) {
for (int i = ; i <= n; i++) add(leftb[i], lefta[i], );
} void update(int id, int l, int r, int x, int y, vector<int>&d) {
if (x <= l && y >= r) {
d.push_back(id);
return;
}
int mid = (l + r) >> ;
if (x <= mid) update(id << , l, mid, x, y, d);
if (y > mid) update(id << | , mid + , r, x, y, d);
}
vector<int>a, b;
int main()
{
scanf("%d%d%d%d", &n, &m, &p, &q);
builda(, , * n), buildb(, , * n), build3( * n);
tot++;
for (int i = ; i <= m; i++) {
int x1, y1, x2, y2, w;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &w);
a.clear(), b.clear();
update(, , * n, x1, y1, a);
update(, , * n, x2 + n, y2 + n, b);
int lena = a.size(), lenb = b.size();
for(int j=;j<lena;j++)
{
int id = numa[a[j]];
add(id, tot, );
}
add(tot, tot + , w);
for(int j=;j<lenb;j++)
{
int id = numb[b[j]];
add(tot + , id, );
}
tot += ;
for(int j=;j<lenb;j++)
{
int id = numa[b[j]];
add(id, tot, );
}
add(tot, tot + , w);
for(int j=;j<lena;j++)
{
int id = numb[a[j]];
add(tot + , id, );
}
tot += ;
}
for (int i = ; i <= p; i++) scanf("%d", &endss[i]);
for (int i = ; i <= q; i++) scanf("%d", &start[i]);
dijkstra();
ll ans = ;
for(int i=;i<=p;i++)
{
int id = lefta[endss[i]];
ans = max(ans, d[id]);
}
if (ans >= inf64) printf("boring game\n");
else printf("%lld\n", ans);
return ;
}
G. 神圣的 F2 连接着我们 线段树优化建图+最短路的更多相关文章
- CodeForces 786B Legacy(线段树优化建图+最短路)
[题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...
- 【BZOJ4276】[ONTAK2015]Bajtman i Okrągły Robin 线段树优化建图+费用流
[BZOJ4276][ONTAK2015]Bajtman i Okrągły Robin Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2 ...
- BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流
BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流 Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1 ...
- 洛谷3783 SDOI2017 天才黑客(最短路+虚树+边转点+线段树优化建图)
成功又一次自闭了 怕不是猪国杀之后最自闭的一次 一看到最短路径. 我们就能推测这应该是个最短路题 现在考虑怎么建图 根据题目的意思,我们可以发现,在本题中,边与边之间存在一些转换关系,但是点与点之间并 ...
- 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)
题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...
- Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )
题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...
- bzoj5017 [Snoi2017]炸弹 (线段树优化建图+)tarjan 缩点+拓扑排序
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5017 题解 这个题目方法挺多的. 线段树优化建图 线段树优化建图的做法应该挺显然的,一个炸弹能 ...
- 7月13日考试 题解(DFS序+期望+线段树优化建图)
T1 sign 题目大意:给出一棵 N 个节点的树,求所有起点为叶节点的有向路径,其 上每一条边权值和的和.N<=10000 水题.考试的时候毒瘤出题人(学长orz)把读入顺序改了一下,于是很多 ...
随机推荐
- 数据结构和算法(Golang实现)(8.2)基础知识-分治法和递归
分治法和递归 在计算机科学中,分治法是一种很重要的算法. 字面上的解释是分而治之,就是把一个复杂的问题分成两个或更多的相同或相似的子问题. 直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合 ...
- 自动补全、回滚!介绍一款可视化 sql 诊断利器
Yearning简介 ================= Yearning MYSQL 是一个SQL语句审核平台.提供查询审计,SQL审核等多种功能,支持Mysql,可以在一定程度上解决运维与开发之间 ...
- 【转】解决存储过程执行快,但C#程序调用执行慢的问题
这两天遇到一个问题令人比较郁闷,一个大概120行左右的存储过程在SQL Server2012的查询分析器里面执行,速度非常理想,1秒不到,即可筛选抓取到大概500条数据记录.但在C#程序代码里调用,就 ...
- Python-selenium 元素定位
1.id定位find_element_by_id() 通过id属性定位元素,如果id是动态变化的话不能用id来进行定位 2.name定位find_element_by_name() 通过name属性定 ...
- 接口测试中实际发生的几个问题——python中token传递
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:AFKplayer PS:如有需要Python学习资料的小伙伴可以加点 ...
- The equation SGU - 106
题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...
- GCD - Extreme (II) UVA - 11426 欧拉函数与gcd
题目大意: 累加从1到n,任意两个数的gcd(i,j)(1=<i<n&&i<j<=n). 题解:假设a<b,如果gcd(a,b)=c.则gcd(a/c,b ...
- jQuer实时监控input对table进行筛选
记得以前写过一个预定表格~~~~~比这个更难,一大串前端js~~~忘了~~~好记性不如烂笔头~~记录下,既帮助别人,也帮助自己~~~ 实现思路~通过.on监听input标签的内容变化,通过this获取 ...
- Xshell 中文提示乱码
1.Alt+P 打开配置对话框,点击终端->编码,选择Unicode(utf-8)编码
- Cucumber(1) —— 环境配置
目录 学习资料 cucumber简介 cucumber环境配置 学习资料 1.cucumber官方学习网站 cucumber简介 1.cucumber是一种支持BBD(behavior-driven ...