4152: [AMPPZ2014]The Captain
4152: [AMPPZ2014]The Captain
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 1561 Solved: 620
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 2
1 1
4 5
7 1
6 7
Sample Output
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define mp(a,b) make_pair(a,b)
#define pa pair<long long ,int>
using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
priority_queue< pa,vector<pa>,greater<pa> >q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void dij() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push(mp(,));
dis[] = ;
while (!q.empty()) {
pa x = q.top();q.pop();
int u = x.second;
if (vis[u]) continue;vis[u] = true;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;;
if (dis[v]>dis[u]+w) {
dis[v] = dis[u] + w;
q.push(mp(dis[v],v));
}
}
}
}
int main() {
freopen("1.txt","r",stdin);
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
dij();
printf("%d",dis[n]);
return ;
}
spfa被卡了QwQ
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
queue<int>q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void spfa() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push();
dis[] = ;
vis[] = true;
while (!q.empty()) {
int u = q.front();q.pop();
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;;
if (dis[v]>dis[u]+w) {
dis[v] = dis[u] + w;
if (!vis[v])q.push(v),vis[v] = true;
}
}
vis[u] = false;
}
}
int main() {
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
spfa();
printf("%d",dis[n]);
return ;
}
4152: [AMPPZ2014]The Captain的更多相关文章
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
- BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心
Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...
- 【BZOJ4152】[AMPPZ2014]The Captain 最短路
[BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...
- BZOJ4152:[AMPPZ2014]The Captain——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...
随机推荐
- java之struts框架入门教程
本教程主要讲述struts的简单入门操作 使用的是myeclipse工具 1.创建web项目 2.复制struts必要的jar包到 WebRoot/WEB-INF/lib 下 jar包列表如下: as ...
- 面向对象(OOP)二
一.“魔术”函数 - 自动调用 魔术方法 在面向对象有一些特别的方法,无需特别定义,已自动具备某些功能,例如构造函数__construt,这些方法统称魔术方法,在日后的编程中,可以使用这些方法的特性设 ...
- 1074 食物链 2001年NOI全国竞赛
1074 食物链 2001年NOI全国竞赛 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 动物王国中有三类动物 ...
- WIn10 电脑运行Docker
参考地址: https://www.cnblogs.com/linjj/p/5606687.html https://docs.docker.com/engine/reference/commandl ...
- cocos2d-x-2.2.0_win7+vs2010搭建_eclipse+ndk-r9+cygwin搭建_教程以及编译问题汇总
声明:我是才用c/c++和cocos2d-x的如果有错误欢迎指出 文章内容我亲测过可以通过,同时我也会一直更新内容 感谢那些把自己的东西分享出来的人 原文地址:http://www.cnblogs.c ...
- 永洪BI——国内领军的一站式大数据分析平台
平台: CentOS 类型: 虚拟机镜像 软件包: jdk-7.79-linux yonghongbi.sh basic software big data business intelligence ...
- 华为服务器操作系统EulerOS V2.0
平台: linux 类型: 虚拟机镜像 软件包: java-1.8.0 php-5.4.16 python-2.7.5 qt-4.8.5 tomcat-7.0.69 basic software eu ...
- javascript HTML静态页面传值的四种方法
一:JavaScript静态页面值传递之URL篇能过URL进行传值.把要传递的信息接在URL上.Post.htm 代码如下: <input type="text" name= ...
- 那些年,被我蠢哭了的php代码小错误~~~
首先,我爱敲代码!!!而且我很喜欢修改bug,在看到那些bug的时候,我是兴奋的,毕竟当你解决这个bug之后感觉是很爽的. 在学习的过程中,看到无数的bug,有一些错误是很微小的,一般在PHP中都能通 ...
- 运维如何延续自己的职业生涯--萧田国2017年GOPS深圳站演讲内容
正如 萧田国在2017年GOPS深圳站演讲所提及的,运维职业生涯规划,应该是T字型. 关于指导原则,正如腾讯好友@赵建春所言: 如果一个领域不能做到TOP,那就是一种伤害. 运维在编程.开发领域,能做 ...