【题目链接】

点击打开链接

【算法】

按x轴排序,将相邻点连边

按y轴排序,将相邻点连边

然后对这个图跑最短路就可以了,笔者用的是dijkstra算法

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200000 struct info {
int id,x,y;
} a[MAXN+]; int N,i;
int dist[MAXN+];
vector< pair<int,int> > E[MAXN+]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline bool cmp1(info a,info b) { return a.x < b.x; }
inline bool cmp2(info a,info b) { return a.y < b.y; } inline void add_edge(int x,int y,int d) {
E[x].push_back(make_pair(y,d));
E[y].push_back(make_pair(x,d));
} inline void dijkstra() {
int i,x,to,cost;
static int vis[MAXN+];
priority_queue< pair<int,int> > q;
for (i = ; i <= N; i++) dist[i] = INT_MAX;
q.push(make_pair(,));
while (!q.empty()) {
x = q.top().second; q.pop();
if (vis[x]) continue;
for (i = ; i < E[x].size(); i++) {
to = E[x][i].first;
cost = E[x][i].second;
if (dist[x] + cost < dist[to]) {
dist[to] = dist[x] + cost;
q.push(make_pair(-dist[to],to));
}
}
}
} int main() { read(N);
for (i = ; i <= N; i++) {
read(a[i].x); read(a[i].y);
a[i].id = i;
}
sort(a+,a+N+,cmp1);
for (i = ; i <= N; i++) add_edge(a[i-].id,a[i].id,a[i].x-a[i-].x);
sort(a+,a+N+,cmp2);
for (i = ; i <= N; i++) add_edge(a[i-].id,a[i].id,a[i].y-a[i-].y); dijkstra();
writeln(dist[N]); return ; }

【AMPPZ 2014】 The Captain的更多相关文章

  1. 【JSOI 2014】序列维护

    [题目链接] 点击打开链接 [算法] 线段树 注意标记下传 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 5 ...

  2. 【SDOI 2014】 旅行

    [题目链接] 点击打开链接 [算法] 树链剖分 每个宗教建一棵线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace s ...

  3. 【TJOI 2014】 上升子序列

    [题目链接] 点击打开链接 [算法] 先考虑50分的做法 : f[i]表示以i结尾的本质不同的上升子序列的个数 则f[i] = sigma(f[j]) (j < i,a[j] < a[i] ...

  4. 【PA 2014】Kuglarz

    [题目链接]            点击打开链接 [算法]            sum[i]表示前i个杯子中,杯子底下藏有球的杯子总数            那么,知道[i,j]这段区间中,藏有球的 ...

  5. 【LNOI 2014】 LCA

    [题目链接] 点击打开链接 [算法] 考虑求lca(x,y)的深度 我们可以将从根到x路径上的点都打上标记,然后,询问y到根上路径的权值和 那么,求sigma(depth(lca(i,z)))(l & ...

  6. 【JLOI 2014】 松鼠的新家

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3631 [算法] 树上差分 [代码] #include<bits/stdc++. ...

  7. 【NOI 2014】 动物园

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3670 [算法] KMP [代码] #include<bits/stdc++.h ...

  8. 【BZOJ 3876】【AHOI 2014】支线剧情

    http://www.lydsy.com/JudgeOnline/problem.php?id=3876 这道题每条支线的意思是每条边... 那么每条边的下界设为1就行了. 这样建出一个DAG,每条边 ...

  9. 【UOJ #29】【IOI 2014】holiday

    http://uoj.ac/problem/29 cdq四次处理出一直向左, 一直向右, 向左后回到起点, 向右后回到起点的dp数组,最后统计答案. 举例:\(fi\)表示一直向右走i天能参观的最多景 ...

随机推荐

  1. meta标签集

    html中的meta总结: 0.声明文档使用的字符编码: <meta charset='utf-8'/> 1.优先使用 IE 最新版本和 Chrome : <meta http-eq ...

  2. android 程序退出的对话框

    package com.example.yanlei.yl; import android.graphics.Color; import android.support.v7.app.AppCompa ...

  3. 【kotlin】long转化为date类型 或者date字符串

    1.方法体中的 package org.joda.time.DateTime(long类型) fun Long?.toDateTime() = if (null != this) DateTime(t ...

  4. 边看chromium的代码,边想骂人...

    这一年一直在看chromium for android的代码,边看边想骂,谷歌这帮人..一开始搞了个牛逼的架构,在安卓4.4上把以前android webkit团队的简单版替换掉了,结果发现性能大不如 ...

  5. Multiple analytic account plans多辅助核算方案

    定义核算方案     菜单 会计/设置/辅助核算会计/多个方案         点击"创建"按钮         说明 辅助核算方案,输入方案名称     点击"添加一个 ...

  6. linux下ndk编译命令行程序及配置

    1.在http://developer.android.com/tools/sdk/ndk/index.html下载Android-ndk-r8e-linux-x86.tar.bz2,解压后把andr ...

  7. 键盘HOOK显示按键信息

    GetKeyNameText(MapVirtualKey(iKeyValue,0)<<16));//iKeyValue 的值为 VK_ESCAPE 等 LRESULT CALLBACK L ...

  8. Git使用之Permission Denied问题解决

    今天碰到了Git的Permission Denied问题. 在安装好git之后,我们通常会配置username和邮箱 git config --global user.name "zengj ...

  9. Does Hadoop require SSH?

    https://wiki.apache.org/hadoop/FAQ#Does_Hadoop_require_SSH.3F Hadoop provided scripts (e.g., start-m ...

  10. C++中的const完全解析

    1. const修饰普通变量和指针 const修饰变量,一般有两种写法:const TYPE value;TYPE const value; 这两种写法在本质上是一样的.它的含义是:const修饰的类 ...