【AMPPZ 2014】 The Captain
【题目链接】
【算法】
按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的更多相关文章
- 【JSOI 2014】序列维护
[题目链接] 点击打开链接 [算法] 线段树 注意标记下传 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 5 ...
- 【SDOI 2014】 旅行
[题目链接] 点击打开链接 [算法] 树链剖分 每个宗教建一棵线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace s ...
- 【TJOI 2014】 上升子序列
[题目链接] 点击打开链接 [算法] 先考虑50分的做法 : f[i]表示以i结尾的本质不同的上升子序列的个数 则f[i] = sigma(f[j]) (j < i,a[j] < a[i] ...
- 【PA 2014】Kuglarz
[题目链接] 点击打开链接 [算法] sum[i]表示前i个杯子中,杯子底下藏有球的杯子总数 那么,知道[i,j]这段区间中,藏有球的 ...
- 【LNOI 2014】 LCA
[题目链接] 点击打开链接 [算法] 考虑求lca(x,y)的深度 我们可以将从根到x路径上的点都打上标记,然后,询问y到根上路径的权值和 那么,求sigma(depth(lca(i,z)))(l & ...
- 【JLOI 2014】 松鼠的新家
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3631 [算法] 树上差分 [代码] #include<bits/stdc++. ...
- 【NOI 2014】 动物园
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3670 [算法] KMP [代码] #include<bits/stdc++.h ...
- 【BZOJ 3876】【AHOI 2014】支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 这道题每条支线的意思是每条边... 那么每条边的下界设为1就行了. 这样建出一个DAG,每条边 ...
- 【UOJ #29】【IOI 2014】holiday
http://uoj.ac/problem/29 cdq四次处理出一直向左, 一直向右, 向左后回到起点, 向右后回到起点的dp数组,最后统计答案. 举例:\(fi\)表示一直向右走i天能参观的最多景 ...
随机推荐
- Ajax 实现文件的下载
JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...
- IO流(1)-键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
1.先写一个Student类 public class Student { private String name; private int chinese; private int math; pr ...
- 深入理解javascript之设计模式
设计模式 设计模式是命名.抽象和识别对可重用的面向对象设计实用的的通用设计结构. 设计模式确定类和他们的实体.他们的角色和协作.还有他们的责任分配. 每个设计模式都聚焦于一个面向对象的设计难题或问题. ...
- Linux中修改docker镜像源及安装docker
1.首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.re ...
- [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
Do you get lost when working with functions and the new keyword? Prototypal inheritance can be compl ...
- BZOJ 题目1036: [ZJOI2008]树的统计Count(Link Cut Tree,改动点权求两个最大值和最大值)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8421 Solved: 3439 [Submi ...
- Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life
Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life ...
- TListView使用方法1(转)
ListView1.Items 为标准 Tlistitems类 ListView1.Items (1)赋值 with ListView1.Items.Add do begin Caption:=cap ...
- 《Getting Started with WebRTC》第二章 WebRTC技术介绍
<Getting Started with WebRTC>第二章 WebRTC技术介绍 本章作WebRTC的技术介绍,主要讲下面的概念: . 怎样建立P2P的通信 . 有效的信 ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...