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