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. 【Java面试题】-- Java String

    Java String 2019-11-02  17:40:45  by冲冲 1.String的内存位置 String是定义在 java.lang 包下的一个类.它不是基本数据类型.String是不可 ...

  2. LOJ #2185 / 洛谷 P3329 - [SDOI2015]约数个数和(莫比乌斯函数)

    LOJ 题面传送门 / 洛谷题面传送门 题意: 求 \(\sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)\),\(d(x)\) 为 \(x\) 的约数个数. \( ...

  3. 对 SAM 和 PAM 的一点理解

    感觉自己学 SAM 的时候总有一种似懂非懂.云里雾里.囫囵吞枣.不求甚解的感觉,是时候来加深一下对确定性有限状态自动机的理解了. 从 SAM 的定义上理解:SAM 可以看作一种加强版的 Trie,它可 ...

  4. 1D RKDG to shallow water equations

    RKDG to shallow water equations 1.Governing Equations \[\frac{\partial U}{\partial t} + \frac{\parti ...

  5. 用Rsync实现windows下同步linux服务器的数据

    一:环境 1.服务端:Red Hat Enterprise Linux Server release 6.4 (Santiago) 2.客户端:windows7旗舰版64位 3.同步对象:测试数据 4 ...

  6. javaWeb - 3 — JSP (技术已淘汰)— 更新完毕

    1.jsp 在servlet中说过java中前端到后台有两条路线嘛 后台 <------ ajax.json <--------- 前端 后台 <------ jsp( EL.JST ...

  7. A Child's History of England.33

    To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...

  8. acknowledge

    accord+knowledge. accord好几个意思,knowledge不遑多让,We gotta acknowledge the word acknowledge has many meani ...

  9. 一道题目学ES6 API,合并对象id相同的两个数组对象

    var arr2=[{id:1,name:'23'}] var arr1=[{id:1,car:'car2'}] const combined = arr2.reduce((acc, cur) =&g ...

  10. 面试一定会问到的-js事件循环

    这篇文章讲讲浏览器的事件循环(nodejs中的事件循环稍有不同),事件循环是js的核心之一,因为js是单线程,所以异步事件实现就是依赖于事件循环机制,理解事件循环可让我们更清晰的处理js异步事件和应对 ...