传送门:

这是一道bzoj权限题

Luogu团队题链接

解题思路

直接连边的话边数肯定会爆炸,考虑减少边数。

我们画出坐标系,发现一个东西:

对于两个点 \(A,B\),\(|x_A-y_A|\) 可以经由由他们俩之间的若干点取到,\(y\) 同理。

所以我们只需要先把所有点分别按照 \(x\) 和 \(y\),相邻两点之间连边即可。

细节注意事项

  • 不要写挂最短路

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 200010;
const int __ = 800010; int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, d); } int n, vis[_]; LL dis[_];
struct node { int x, y, id; }p[_]; inline bool cmp1(const node& a, const node& b) { return a.x < b.x; } inline bool cmp2(const node& a, const node& b) { return a.y < b.y; } inline void Dijkstra() {
priority_queue < pair < LL, int > > Q;
memset(dis, 0x3f, sizeof dis);
dis[1] = 0, Q.push(make_pair(0, 1));
while (!Q.empty()) {
pair < LL, int > x = Q.top(); Q.pop();
int u = x.second;
if (vis[u]) continue; vis[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[v] > dis[u] + w[i])
dis[v] = dis[u] + w[i], Q.push(make_pair(-dis[v], v));
}
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(p[i].x), read(p[i].y), p[i].id = i;
sort(p + 1, p + n + 1, cmp1);
for (rg int i = 1; i < n; ++i) link(p[i].id, p[i + 1].id, p[i + 1].x - p[i].x);
sort(p + 1, p + n + 1, cmp2);
for (rg int i = 1; i < n; ++i) link(p[i].id, p[i + 1].id, p[i + 1].y - p[i].y);
Dijkstra();
printf("%lld\n", dis[n]);
return 0;
}

完结撒花 \(qwq\)

最新文章

  1. USB 设备的PID-Product ID,VID-Vendor ID
  2. Tomcat源码分析之—容器整体结构
  3. eclipse配置gradle
  4. JS之延迟处理
  5. PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
  6. python多态
  7. mysql函数计算地表两点间距离
  8. C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程
  9. 第二十九篇、UICollectionView瀑布流
  10. 3DMAX-模型制作简介
  11. http soap关系
  12. 我的Python成长之路---第六天---Python基础(19)---2016年2月20日(晴)
  13. BZOJ 1449 JSOI2009 球队收益 费用流
  14. ubuntu下安装teamviewer
  15. Scala 安装 Exception in thread &quot;main&quot; java.lang.VerifyError: Uninitialized object exists on backward branch 96
  16. 【转】配置Exchange 2010 服务器(二)Exchange2010证书配置
  17. 自动检测ie低版本,并显示升级浏览器的自定义页面,当用f12再把浏览器版本提高的时候,又会自动显示正常的页面。
  18. 原码,反码与补码的概念以及Java中数的存储方式
  19. C/C++ 查看数组类型长短 , python的len的封装原理
  20. thinkphp5部署在宝塔面板问题!

热门文章

  1. 1、TensorFlow如何工作?
  2. websocket协议实现
  3. Linux下载安装
  4. 操作Document文档
  5. VS2017中使用C++语言编写delay函数实现延迟
  6. windows server 2016系统激活
  7. JDBC statement的常用方法
  8. centos 8 cockpit系统监控
  9. JQuery DOM操作:设置内容&amp;属性&amp;添加元素&amp;插入元素&amp;包裹&amp;克隆&amp;移除&amp;替换
  10. vs2017 vs2019配置sqlite3连接引擎(驱动)指南(二)vs2019续集