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 ...
随机推荐
- 2017年最有前景的十大IT职业岗位
在IT行业,并不常存在失业的现象,因为目前整个行业存在严重的专业人才供给不足的现象:但同样,想要进入这个行业并牢牢站稳脚跟,你也需要拥有更强于其他行业的竞争力和承受更大的压力.那在行业中,哪些职位更有 ...
- SNP 过滤(一)
通用过滤 Vcftools(http://vcftools.sourceforge.net) 对vcf文件进行过滤 第一步:过滤最低质量低于30,次等位基因深度(minor allele count) ...
- getdelim函数
利用getdelim函数分割读取字段,将文件制表符替换为空格符 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main( ...
- socket编程:多路复用I/O服务端客户端之select
其实在之前的TCP之中,我们编程实现了多进程,多线程机制下的TCP服务器,但是对于这种的TCP服务器而言,存在太大的资源局限性.所以我们可以是用I/0模型中的多路复用I/O模型来进行编程. 他的具体思 ...
- Docker镜像相关操作
批量导入镜像 ll *.tgz|awk '{print $NF}'|sed -r 's#(.*)#docker load -i \1#' |bash 批量打tag docker images | se ...
- sed 修改文件
总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...
- 网易云信 集成UI库登录dologin没有回调
感谢github上的两位大佬指出问题的解决方法. 解决方法: 在进行ui初始化要在主进程中进行,初始化前进行主进程判断. 若还收不到回调,可尝试将uikit中的base包去掉而在build.gradl ...
- 日常Java 2021/9/28
字符串反转 package m; public class m { public static void main(String[] args) { //定义一个字符串 String str = &q ...
- account, accomplish, accumulate
account account从词源和count(数数)有关,和computer也有点关系.calculate则和'stone used in counting'有关.先看两个汉语的例子:1. 回头再 ...
- Sharding-JDBC 实现水平分库分表
1.需求分析