AT4811 [ABC160D] Line++ 题解
Content
给定一个 \(n\) 个点、\(n\) 条边的无向图。对于所有的 \(1\leqslant i<n\),在点 \(i,i+1\) 之间连一条无向边。另外在给定两个点 \(x,y\),在点 \(x,y\) 之间连一条无向边。现请对于所有的 \(1\leqslant k<n\),求出图中最短距离为 \(k\) 的点对数。
数据范围:\(3\leqslant n\leqslant2\times 10^3\),\(1\leqslant x,y\leqslant n\),\(x+1<y\)。
Solution
相信各位一开始想到的就是最短路算法了吧。但是 \(\mathcal O(n^3)\) 的 Floyd 算法并不能跑得过去,那么自然就去想别的最短路算法了,比如 Dijkstra。这时我们发现,\(\mathcal O(n\log n)\) 的堆优化 Dijkstra 貌似可以较轻松地通过此题,那我们不妨来继续往下想一下!
首先我们对于每个点用堆优化 Dijkstra 求出其到每个点的距离,然后再将每个距离存储到桶中。枚举每个点是 \(\mathcal O(n)\) 的,每次跑堆优化 Dijkstra 是 \(\mathcal O(n\log n)\) 的,因此总的时间复杂度是 \(\mathcal O(n^2\log n)\) 的,足以通过此题。
注意,这么样来的话,我们距离为 \(k\) 的点对 \((x,y)\) 就会重复算 \(2\) 次(一次在从点 \(x\) 跑 Dijkstra 的时候,另一次在从点 \(y\) 跑 Dijkstra 的时候)。因此我们最后要将每个得出来的距离为 \(k\) 的点对数除以 \(2\),才能得到正确结果。
Code
const int N = 2e3 + 7;
int n, cnt, num[N], h[N << 1], dis[N], vis[N];
struct edge {int to, nxt;}e[N << 1];
iv a_e(int u, int v) {e[++cnt] = (edge){v, h[u]}; h[u] = cnt;}
iv dj(int s) {
pq<pii> q;
dis[s] = 0, q.push(pii(0, s));
while(!q.empty()) {
int x = q.top().se; q.pop();
if(vis[x]) continue;
vis[x] = 1;
for(int i = h[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > dis[x] + 1) dis[y] = dis[x] + 1, q.push(pii(-dis[y], y));
}
}
}
int main() {
n = Rint;
F(int, i, 1, n - 1) a_e(i, i + 1), a_e(i + 1, i);
int x = Rint, y = Rint; a_e(x, y), a_e(y, x);
F(int, i, 1, n) {
memset(dis, 0x3f, sizeof(dis)), memset(vis, 0, sizeof(vis));
dj(i);
F(int, j, 1, n) num[dis[j]]++;
}
F(int, i, 1, n - 1) num[i] /= 2, println(num[i]);
return 0;
}
AT4811 [ABC160D] Line++ 题解的更多相关文章
- [LeetCode] Max Points on a Line 题解
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...
- 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解
题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- Max Points on a Line leetcode java
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- 【leetcode刷题笔记】Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 2016百度之星 初赛2A ABEF
只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...
- [HDU] 3711 Binary Number [位运算]
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...
- Airport Express UVA - 11374
In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more q ...
随机推荐
- freeswitch verto communicator客户端
概述 我们在web客户端使用sip协议时用的比较多的是sipml5库和jssip库. 但是sip协议比较重,又复杂,所以freeswitch内部就自定义了一个verto协议,方便在web页面上使用音视 ...
- led汇编点灯
1. 汇编LED原理 为什么使用Cortex-A汇编 使用汇编初始化soc外设 使用汇编初始化DDR,I.MX不需要,因为它内部的96k ROM中存放了自己编写的启动代码,这些代码可以读取DDR配置信 ...
- 从零开始学Kotlin第二课
字符串模板 fun main(args:Array<String>){ //主函数main方法 println(diaryGenerater("天安门")); } // ...
- SpringCloud升级之路2020.0.x版-44.避免链路信息丢失做的设计(2)
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们在这一节我们将继续讲解避免链路信息丢失做的设计,主要针对获取到现有 Span 之后,如 ...
- 洛谷 P4099 - [HEOI2013]SAO(树形 dp)
题面传送门 题意: 有一个有向图 \(G\),其基图是一棵树 求它拓扑序的个数 \(\bmod (10^9+7)\) \(n \in [1,1000]\) 如果你按照拓扑排序的方法来做,那恐怕你已经想 ...
- pycurl报错: ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend
报错: ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl ...
- SNPEFF snp注释 (添加自己基因组)
之间介绍过annovar进行对snp注释,今天介绍snpEFF SnpEff is a variant annotation and effect prediction tool. It annota ...
- 使用Docker编译OpenResty支持国密ssl加密
编译环境 执行编译操作环境如下 #操作系统 CentOS Linux release 7.4.1708 (Core) #docker版本 Version: 19.03.5 编译过程 Dockerfil ...
- 巩固javaweb的第二十八天
巩固内容: 设置页面的编码方式 实现代码: 每个 JSP 页面都需要设置编码方式,设置 JSP 页面的编码方式可以是下面两种方式 之一. 方式一: <%@ page contentType=&q ...
- abide, able, abnormal
abide 近/反义词:1. 忍受: bear, endure, put up with, stand, tolerate2. 遵守(abide by): accept, comply, confor ...