【题目链接】

点击打开链接

【算法】

按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 ; }

最新文章

  1. 04实现累加和计算功能并且实现textbox不允许输入数字以外的字符但不包括退格键同时不允许第一个数值为0
  2. 论文阅读(Xiang Bai——【PAMI2017】An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition)
  3. 算法导论----贪心算法,删除k个数,使剩下的数字最小
  4. 上海SAP代理商 服装行业ERP系统 达策SAP金牌代理商
  5. Messenger信使
  6. Selinux在HTTP+PHP服务中的安全权限修改
  7. 前App Store高管揭秘:关于“苹果推荐”的七大真相
  8. 本机不安装Oracle客户端,使用PL/SQL Developer连接远程数据库
  9. CSS弹性盒模型 box-flex
  10. wuzhicms刷新按钮的功能开发
  11. android110 jni01
  12. Object Pool
  13. 【深入理解Java内存模型】
  14. HTML&amp;CSS_基础01
  15. 阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
  16. Java学习笔记(十八):static关键字
  17. MathWorks官方消息:神经网络工具箱不能编译
  18. 谷歌浏览器使用IE内核
  19. 20145322 Exp5 Adobe阅读器漏洞攻击
  20. P&#183;C&#183;L 了解

热门文章

  1. HTTP请求的缓存(Cache)机制
  2. 创建ROS工作空间和包
  3. LCD1602和LCD12864
  4. 用canvas实现一个colorpicker
  5. 【Android开发—智能家居系列】(三):手机连接WIFI模块
  6. stl_内存基本处理工具
  7. ViewPagerTransforms
  8. busybox 终端支持 ctrl-r
  9. iOS 内购遇到的坑
  10. 甘特图——Excel搞定