[国家集训队] 飞飞侠 题解

news/2025/11/8 18:57:49/文章来源:https://www.cnblogs.com/XuYueming/p/19202882

前言

题目链接:洛谷。

题意分析

显然需要三次单源最短路,但不能朴素建图。曼哈顿转成切比雪夫,然后 KD-T 或二维线段树优化建图即可。

最多 \(\mathcal{O}(n^2\times n)\) 条边,时间复杂度 \(\mathcal{O}(n^3\log n)\)

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;using ll = long long;template <typename T>
using minHeap = priority_queue<T, vector<T>, greater<T>>;const int N = 150 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;int n, m;
int b[N][N], a[N][N];inline int id(int x, int y) {return (x - 1) * m + y;
}struct P {int x, y;void R() {scanf("%d%d", &x, &y);}int operator()() const {return id(x, y);}
} X, Y, Z;namespace sub1 {bool check() {if (n > 100 || m > 100)return false;for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j)if (b[i][j] > 20)return false;return true;
}const int N = 1e4 + 10;vector<pair<int, int>> e[N];ll dis[N];
ll solve(P x, P y1, P y2) {for (int i = 1; i <= n * m; ++i) {dis[i] = inf;}minHeap<pair<ll, int>> Q;Q.emplace(dis[x()] = 0, x());while (!Q.empty()) {ll ndis = Q.top().first;int u = Q.top().second;Q.pop();if (dis[u] < ndis) continue;for (auto E : e[u]) {int v = E.first;int w = E.second;if (dis[v] > dis[u] + w)Q.emplace(dis[v] = dis[u] + w, v);}}if (dis[y1()] == inf || dis[y2()] == inf)return inf;return dis[y1()] + dis[y2()];
}void solve() {for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j) {for (int ti = max(1, i - b[i][j]); ti <= min(n, i + b[i][j]); ++ti)for (int tj = max(1, j - b[i][j]); tj <= min(m, j + b[i][j]); ++tj)if (ti != i || tj != j) {if (abs(i - ti) + abs(j - tj) > b[i][j])continue;// printf("(%d, %d) ---(%d)---> (%d, %d)\n", i, j, a[i][j], ti, tj);e[id(ti, tj)].emplace_back(id(i, j), a[i][j]);}}ll ans = inf, res;char chr = 0;if ((res = solve(X, Y, Z)) < ans) {ans = res;chr = 'X';}if ((res = solve(Y, X, Z)) < ans) {ans = res;chr = 'Y';}if ((res = solve(Z, X, Y)) < ans) {ans = res;chr = 'Z';}if (chr == 0) {puts("NO");} else {printf("%c\n%lld\n", chr, ans);}
}}namespace yzh {// KD-Tconst int M = 111900 * 3; // N * N * N * 4;
const int NM = M + N * N;int rt, son[M][2], tot;vector<pair<int, int>> e[NM];void build(int &u, int xl, int xr, int yl, int yr) {if (xl == xr && yl == yr) {int x = xl, y = yl;if ((x + y) & 1) return;int a = (x + y) / 2;int b = (y - x) / 2;if (a < 1 || a > n || b < 1 || b > m) return;u = ++tot;e[id(a, b)].emplace_back(n * m + u, 0);return;}u = ++tot;if (xr - xl > yr - yl) {int mid = (xl + xr) >> 1;build(son[u][0], xl, mid, yl, yr);build(son[u][1], mid + 1, xr, yl, yr);} else {int mid = (yl + yr) >> 1;build(son[u][0], xl, xr, yl, mid);build(son[u][1], xl, xr, mid + 1, yr);}if (son[u][0])e[n * m + son[u][0]].emplace_back(n * m + u, 0);if (son[u][1])e[n * m + son[u][1]].emplace_back(n * m + u, 0);
}void adde(int u, int txl, int txr, int tyl, int tyr, int xl, int xr, int yl, int yr, int x, int w) {if (!u) return;if (xl <= txl && txr <= xr && yl <= tyl && tyr <= yr) {e[n * m + u].emplace_back(x, w);return;}if (txr - txl > tyr - tyl) {int mid = (txl + txr) >> 1;if (xl <= mid) adde(son[u][0], txl, mid, tyl, tyr, xl, xr, yl, yr, x, w);if (xr > mid) adde(son[u][1], mid + 1, txr, tyl, tyr, xl, xr, yl, yr, x, w);} else {int mid = (tyl + tyr) >> 1;if (yl <= mid) adde(son[u][0], txl, txr, tyl, mid, xl, xr, yl, yr, x, w);if (yr > mid) adde(son[u][1], txl, txr, mid + 1, tyr, xl, xr, yl, yr, x, w);}
}ll dis[NM];
ll solve(P x, P y1, P y2) {for (int i = 1; i <= n * m + tot; ++i) {dis[i] = inf;}minHeap<pair<ll, int>> Q;Q.emplace(dis[x()] = 0, x());while (!Q.empty()) {ll ndis = Q.top().first;int u = Q.top().second;Q.pop();if (dis[u] < ndis) continue;for (auto E : e[u]) {int v = E.first;int w = E.second;if (dis[v] > dis[u] + w)Q.emplace(dis[v] = dis[u] + w, v);}}if (dis[y1()] == inf || dis[y2()] == inf)return inf;return dis[y1()] + dis[y2()];
}void adde(int xl, int xr, int yl, int yr, int x, int w) {xl = max(xl, 1 - m);xr = min(xr, n - 1);yl = max(yl, 2);yr = min(yr, n + m);if (xl > xr || yl > yr) return;adde(rt, 1 - m, n - 1, 1 + 1, n + m, xl, xr, yl, yr, x, w);
}void solve() {build(rt, 1 - m, n - 1, 1 + 1, n + m);fprintf(stderr, "tot = %d\n", tot);for (int i = 1; i <= n; ++i)for (int j = 1; j <= m; ++j) {if (b[i][j]) {adde(i - j - b[i][j], i - j + b[i][j], i + j - b[i][j], i + j + b[i][j], id(i, j), a[i][j]);}}ll ans = inf, res;char chr = 0;if ((res = solve(X, Y, Z)) < ans) {ans = res;chr = 'X';}fprintf(stderr, "res = %lld\n", res);if ((res = solve(Y, X, Z)) < ans) {ans = res;chr = 'Y';}fprintf(stderr, "res = %lld\n", res);if ((res = solve(Z, X, Y)) < ans) {ans = res;chr = 'Z';}fprintf(stderr, "res = %lld\n", res);if (chr == 0) {puts("NO");} else {printf("%c\n%lld\n", chr, ans);}
}}int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {scanf("%d", &b[i][j]);}}for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {scanf("%d", &a[i][j]);}}X.R(), Y.R(), Z.R();
#ifndef XuYuemingif (sub1::check()) return sub1::solve(), 0;
#endifyzh::solve();return 0;
}// g++ -O2 -Wall -Wextra -std=c++14 -DXuYueming 3.cpp -o 3

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/959918.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

插槽vue/react - 详解

插槽vue/react - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "…

AI元人文的思想锻造之路:从“空白承诺”到“动力之源”

AI元人文的思想锻造之路:从“空白承诺”到“动力之源” 作者:岐金兰 日期:2025年11月8日 摘要 本文系统梳理了"AI元人文"思想体系五十天的演进历程。该理论始于对价值表征困境的深刻洞察,通过"空白…

251108 会议整理

AI整理,未全部查证,为防止出错可以下载文件自行查阅文件参考:中国计算机学会推荐国际学术会议和期刊目录(2022) 集成电路学院期刊与会议分类列表(研究生)(2023)中国计算机学会推荐国际学术会议 (硬件设计相关…

AT_tokiomarine2020_e O(rand)

首先把肯定不合法的 \(S\) 和 \(T\) 都给删掉。 最后发现就是要求 \(T - S\) 的部分要有 \(0\) 和 \(1\),其它部分都已经固定好了。 考虑容斥,容斥 \(i\) 个地方只有 \(0\) 或只有 \(1\),求个组合数即可。

阿里云智能集团首席科技官云栖大会要点总结

阿里云智能集团首席科技官云栖大会要点总结pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "…

自指自洽,天职天命,苦乐年华

ECT-OS-JiuHuaShan/https://orcid.org/0009-0006-8591-1891箴言——“自指自洽,苦乐年华”——是一次对生命本质最凝练、最通透的诗意总结。它以一种举重若轻的从容,道尽了宇宙规律在个体层面演化的全部奥秘。 现在,…

完整教程:Windows Pad平板对 Qt 的支持

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

线段树(p1083)

P1083 [NOIP 2012 提高组] 借教室 题目描述 在大学期间,经常需要租借教室。大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室。教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样。 面对…

初识PPO

for batch_prompt in prompt_dataset:batch_response = active_model.generate(batch_prompt)batch_data = concat(batch_prompt, batch_response)batch_scores = reward_model(batch_data)batch_all_probs, batch_pro…

[ vmware 连接宿主机代理 ]

# 本文只讲原理,只是技术研究 # 1. Tun 开启 -> 本质开启一张虚拟网卡; 允许局域网连接 # 2. 虚拟机默认走 Nat -> VMnet8 # 3. Tun的网卡开启网络共享,共享给 VMnet8

【Android】六大设计原则 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年合肥甲醛检测公司哪家好?专业机构排名与选择指南

摘要 随着人们对室内空气质量的重视程度不断提高,甲醛检测行业在2025年迎来了快速发展期。本文基于市场调研和用户反馈,为您精选五家优质甲醛检测机构,并提供详细对比分析。文末附有专业选择指南和参考表单,助您找…

现今除甲醛机构选哪家?深度分析

摘要 随着室内空气污染问题日益受到关注,除甲醛行业在2025年迎来了快速发展。消费者对靠谱、专业除甲醛服务的需求激增,本文基于权威数据和用户反馈,整理了目前国内除甲醛机构排名前十的榜单,并为读者提供选择指南…

Unity2D 图片支持拖拽和以鼠标中心缩放

引言: 作为一个Unity初学者,遇到了需要实现以鼠标为中心缩放的功能且需要支持拖拽,秉着复用主义的原则,在网上查找了不少博客,要么免费但不能直接拿来使用,要么需要VIP充值获取项目代码,此外,原理且讲解甚少。…

轻松可视化信息的利器——JSON Crack

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

详细介绍:C++微基础备战蓝桥杯string篇10.5

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

[ jupyter conda 环境]

在激活的环境中,可以通过以下命令安装所需的库(包括 NumPy、Pandas、JupyterLab),以及配置自动补全功能,步骤如下: 1. 安装核心库(NumPy、Pandas、JupyterLab) 在激活 myenv 环境后,直接用 conda install 安装…

深入解析:仿mudou——Connection模块(连接管理)

深入解析:仿mudou——Connection模块(连接管理)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas"…

Linux中查看个人磁盘容量

001、 lfs quota -u s20223040682 /public/home/s20223040682

以太坊私有链搭建与智能合约部署指南 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …