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++ 题解的更多相关文章

  1. [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 ...

  2. 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解

    题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...

  3. UVA12657 Boxes in a Line:题解

    题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...

  4. 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 ...

  5. 【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. ...

  6. 2016百度之星 初赛2A ABEF

    只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...

  7. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  8. 算法与数据结构基础 - 哈希表(Hash Table)

    Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...

  9. Airport Express UVA - 11374

    In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more q ...

随机推荐

  1. Codeforces 1406E - Deleting Numbers(根分+数论)

    Codeforces 题面传送门 & 洛谷题面传送门 一道个人感觉挺有意思的交互题,本人一开始想了个奇奇怪怪的做法,还以为卡不进去,结果发现竟然过了,而且还是正解( 首先看到这类题目可以考虑每 ...

  2. #pragma warning(disable:4996)

    VS使用中常常会遇到一些过时或者不安全的函数,编译会报错: 错误 C4996 'sscanf': This function or variable may be unsafe. Consider u ...

  3. R2CNN模型——用于文本目标检测的模型

    引言 R2CNN全称Rotational Region CNN,是一个针对斜框文本检测的CNN模型,原型是Faster R-CNN,paper中的模型主要针对文本检测,调整后也可用于航拍图像的检测中去 ...

  4. c#Gridview添加颜色

    e.Row.Cells[1].ForeColor = System.Drawing.Color.Blue;

  5. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  6. adult

    adult是adolescere (grow up)的过去分词. egg - embryo [胚胎] - foetus [就要出生的胎儿] - toddler [刚会走路] - adolescent ...

  7. Leetcode中的SQL题目练习(一)

    595. Big Countries https://leetcode.com/problems/big-countries/description/ Description name contine ...

  8. 链栈(C++)

    链栈,字面意思,就是用链表来实现一个栈的数据结构. 那么,只需将单链表的头节点当作栈顶,尾节点当作栈底.入栈只需要头插,出栈只需头删即可.所以只需要吧单链表稍微阉割一下就可以得到链式栈了.代码如下 / ...

  9. 【♪♪♪】网易云音乐mp3真实地址

    参考别人的博客,得到下面的地址,填上ID号即可,后缀的[.mp3]不用输入 http://music.163.com/song/media/outer/url?id= 例如 最终,合并地址为 http ...

  10. mysql删除数据后不释放空间问题

    如果表的引擎是InnoDB,Delete From 结果后是不会腾出被删除的记录(存储)空间的. 需要执行:optimize table 表名; eg:optimize table eh_user_b ...