传送门:

这是一道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\)

「AMPPZ2014」The Captain的更多相关文章

  1. [题解] [BZOJ4144] 「AMPPZ2014」Petrol

    题面 怎么是权限题啊 题解 有一次考过, 但是不记得了 如果每个点都是加油站的话, 这道题就是货车运输 考虑如何转化 我们可以设

  2. 题解【BZOJ4145】「AMPPZ2014」The Prices

    题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...

  3. 「AMPPZ2014」Petrol

    传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 首先对于每一个点 \(x\) 预处理出 \(nr[x]\) 和 \(dis[x]\),分别表示离 \(x\) 最近的加油站以及该段距离 ...

  4. 「AMPPZ2014」The Prices

    传送门 Luogu团队题链接 解题思路 看到 \(m\) 这么小,马上想到状压 \(\text{DP}\). 设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) ...

  5. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  6. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  7. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  8. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  9. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

随机推荐

  1. Codeforces Round #622 (Div. 2) A. Fast Food Restaurant

    Tired of boring office work, Denis decided to open a fast food restaurant. On the first day he made ...

  2. go get下载包失败问题

    文章引用自 解决go get下载包失败问题 从github克隆 golang在github上建立了一个镜像库,如https://github.com/golang/net就对应是 https://go ...

  3. provide 和 inject高阶使用

    provide 在祖先里授权导出 inject在后代负责接收 foo可以是本组件的函数方法 或者 变量foo 也可以是祖先组件自己 祖先组件foo: this 后代组件 foo.$options.da ...

  4. 《TCP/IP网络编程》读书笔记

    1.Windows 下的 socket 程序和 Linux 思路相同,但细节有所差别(1) Windows 下的 socket 程序依赖 Winsock.dll 或 ws2_32.dll,必须提前加载 ...

  5. C语言:利用指针解决:统计一个长度为2的字符串在另外一个字符串中出现的次数。

    //统计一个长度为2的字符串在另外一个字符串中出现的次数. #include <conio.h> #include <stdio.h> #include <string. ...

  6. windows maven配置

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  7. 「JSOI2013」侦探jyy

    「JSOI2013」侦探jyy 传送门 个人感觉我写的复杂度不够优秀啊,但是好像没有别的办法了... 我们枚举每个点,考虑这个点能不能不发生. 首先我们从这个点开始,在反图上面 \(\text{BFS ...

  8. Python - 静态方法@staticmethod和类方法classmethod

    传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...

  9. Linux 常用命令——查看系统

    有的时候别人给你一个登录方式,但是不知道是啥系统,看图就知道了 1.uname -a    查看电脑以及操作系统 2.cat /proc/version 正在运行的内核版本 3.cat /etc/is ...

  10. 【读书笔记】图解HTTP完整流程

    1.1 使用 HTTP 协议访问 Web: 根据 Web 浏览器地址栏中指定的URL,从服务端获取资源等信息,在页面上展示. Web 使用一种 HTTP (超文本传输协议)作为规范,完成从客户端到服务 ...