4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 1561  Solved: 620
[Submit][Status][Discuss]

Description

给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

Input

第一行包含一个正整数n(2<=n<=200000),表示点数。
接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。
 
 

Output

一个整数,即最小费用。

Sample Input

5
2 2
1 1
4 5
7 1
6 7

Sample Output

2
 

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的更多相关文章

  1. 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain

    循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...

  2. BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )

    先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...

  3. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2107  Solved: 820[Submi ...

  4. bzoj 4152[AMPPZ2014]The Captain

    bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...

  5. BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心

    Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...

  6. bzoj4152[AMPPZ2014]The Captain 最短路

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1517  Solved: 603[Submi ...

  7. BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*

    BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...

  8. 【BZOJ4152】[AMPPZ2014]The Captain 最短路

    [BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...

  9. BZOJ4152:[AMPPZ2014]The Captain——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...

随机推荐

  1. agc016A - Shrinking(字符串 贪心)

    题意 题目链接 给出一个字符串,每次操作可以使得字符串缩短一位,且第$i$位必须要保证与变换前的这一位或下一位相同, 问使得整个字符串全相同最少的操作次数 Sol 300P的题我都要想10min啊,还 ...

  2. 表格<table>

    <table> <tr> <th>表头1</th> <th>表头2</th> <th>表头3</th> ...

  3. 【Java/Android性能优 7】Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    本文转自:http://www.trinea.cn/android/android-common-lib/ 介绍总结的一些android公共库,包含缓存(图片缓存.预取缓存.网络缓存).公共View( ...

  4. 559. N 叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000.树的节点总 ...

  5. SqlServer Alwayson主副本图标显示问号的原因

    搭建完alwayson后,登录辅助副本服务器,查看alwayson可用性副本列表,看到主副本前面显示了一个问号,这里借用网上一张图片做展示: 在显示问号的主副本上右键属性查看,“角色”一栏中,显示的是 ...

  6. Js面向对象之观察者模式

    //模拟一个目标可能拥有的一些列依赖 function ObserverList() { this.observerList = []; }; //添加一个观察者 ObserverList.proto ...

  7. LeetCode Add Two Numbers 两个数相加

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  8. [Asp.Net] Global.asax

    Global.asax.cs文件会被编译到对应的dll 但部署是还需要Global.asax文件  class Global中的方法才会在程序启动时执行

  9. TFS看板晨会

    迭代任务看板 打开任务看板 打开燃尽图查看剩余工作情况,如果离发布较近,但是还有很多剩余工作,可能需要提前准备移除一部分优先级低的需求,如果剩余工作较少,适当安排一些需求 任务板按照人员分组,查看每个 ...

  10. POJ - 3685 Matrix

    二分kth,答案满足的条件为:m ≤ 小于等于x的值数cntx.x和cntx单调不减,随着x增大,条件成立可表示为:0001111. 本地打一个小型的表可以发现列编号j固定时候,目标函数f(i,j)似 ...