「AMPPZ2014」The Captain
传送门:
这是一道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的更多相关文章
- [题解] [BZOJ4144] 「AMPPZ2014」Petrol
题面 怎么是权限题啊 题解 有一次考过, 但是不记得了 如果每个点都是加油站的话, 这道题就是货车运输 考虑如何转化 我们可以设
- 题解【BZOJ4145】「AMPPZ2014」The Prices
题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...
- 「AMPPZ2014」Petrol
传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 首先对于每一个点 \(x\) 预处理出 \(nr[x]\) 和 \(dis[x]\),分别表示离 \(x\) 最近的加油站以及该段距离 ...
- 「AMPPZ2014」The Prices
传送门 Luogu团队题链接 解题思路 看到 \(m\) 这么小,马上想到状压 \(\text{DP}\). 设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
随机推荐
- linux中的diff命令
今天在公司的代码中看到了一个用的不是很多的命令diff,一开始以为不是,后来一查发现还真有这个命令,有关它的详细资料在这个网址中查看[http://blog.chinaunix.net/uid-253 ...
- java ArrayList添加元素全部一样
#开始 今天遇到了一个很神奇的事情 也即是我在用ArrayList的add方法循环加入对象的时候 发现添加的元素全部都是一样的 定位错误定位了一个下午.... 错误位置就是哪一个位置 但是就是不知道为 ...
- 【Hibernate QBC】
HibernateQBC public class HibernateQBC { //演示离线查询 @Test public void testSelect6() { SessionFactory s ...
- oracle 提取文本中的数字
提取文本中的数字部分,并转换为数字 TO_NUMBER(regexp_substr(AGE,'[0-9.]+'))
- Android 调起系统相机拍照
概述 最近在看 nanChen 写的图片选择器 ImagePicker,感觉写得很不错,也打算把从中学到的东西写下来.很多时候,遇到一个好的框架能够降低开发成本这是好事.但是也要去了解其内部具体实现逻 ...
- Java入门笔记 05-多线程
介绍:Java提供了非常优秀的多线程支持,程序可以通过非常简单的方式来启动多线程.本章主要内容为:多线程的创建.启动.控制以及同步操作,并介绍JDK 5新增的线程创建方式. 一.线程的创建与使用: 1 ...
- 源代码管理工具(2)——SVN(2)——第一次用SVN遇到的问题
今天因为项目的需要第一次使用了svn来托管项目,第一使用svn遇到了几个问题. 这个安装的过程很简单,不再赘述.在安装完成之后,相信肯定有一部分第一次用这个的人直接到开始处打开这个软件,这时候就会弹出 ...
- WLC RTU license
目前思科的某些WLC不是一定要license文件去安装,例如这里提到的RTU license. RTU:Right To Use Right to Use (RTU) licensing is a m ...
- 用js实现复制内容到操作系统粘贴板(兼容IE、谷歌、火狐等浏览器)
一.如果只考虑IE浏览器,可以直接用原声js实现 if(window.clipboardData){ //清空操作系统粘贴板 window.clipboardData.clearData(); //将 ...
- NOIP2016换教室 BZOJ 4720
BZOJ 4720 换教室 题目描述: 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上 ...