bzoj4152[AMPPZ2014]The Captain*

Posted YuanZiming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj4152[AMPPZ2014]The Captain*相关的知识,希望对你有一定的参考价值。

bzoj4152[AMPPZ2014]The Captain

题意:

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

题解:

结论:按某维坐标排序后,只有相邻两个点的距离才可能是这两个点的最小距离。故本题只要对所有点先按横坐标排序,将相邻的点连边,再对所有点按纵坐标排序,将相邻的点连边,之后求一次最短路即可。注意,本题数据大,spfa不能过(加了SLF也不行)。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 200010
 7 #define INF 10000000000000000
 8 #define ll long long
 9 using namespace std;
10 
11 inline int read(){
12     char ch=getchar(); int f=1,x=0;
13     while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
14     while(ch>=0&&ch<=9)x=x*10+ch-0,ch=getchar();
15     return f*x;
16 }
17 int n,x[maxn],y[maxn],id[maxn]; struct e{int t; ll w; int n;}es[maxn*4]; int ess,g[maxn];
18 void pe(int f,int t,ll w){
19     es[++ess]=(e){t,w,g[f]}; g[f]=ess; es[++ess]=(e){f,w,g[t]}; g[t]=ess;
20 }
21 bool cmp1(int a,int b){return x[a]<x[b];} bool cmp2(int a,int b){return y[a]<y[b];}
22 struct hn{int u; ll d; bool operator < (const hn&a)const{return d>a.d;}};
23 ll d[maxn]; bool vis[maxn]; priority_queue<hn>q;
24 ll dijkstra(){
25     inc(i,1,n)d[i]=INF; d[1]=0; q.push((hn){1,0});
26     while(!q.empty()){
27         int x; while(!q.empty()&&vis[x=q.top().u])q.pop(); if(vis[x])break; vis[x]=1;
28         for(int i=g[x];i;i=es[i].n)if(d[es[i].t]>d[x]+es[i].w){
29             d[es[i].t]=d[x]+es[i].w; q.push((hn){es[i].t,d[es[i].t]});
30         }
31     }
32     return d[n];
33 }
34 int main(){
35     n=read(); inc(i,1,n)x[i]=read(),y[i]=read(); inc(i,1,n)id[i]=i;
36     sort(id+1,id+n+1,cmp1); inc(i,1,n-1)pe(id[i],id[i+1],abs(x[id[i+1]]-x[id[i]]));
37     sort(id+1,id+n+1,cmp2); inc(i,1,n-1)pe(id[i],id[i+1],abs(y[id[i+1]]-y[id[i]]));
38     printf("%lld",dijkstra()); return 0;
39 }

 

20161108

以上是关于bzoj4152[AMPPZ2014]The Captain*的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ4152[AMPPZ2014]The Captain 最短路

bzoj4152: [AMPPZ2014]The Captain

bzoj4152 [AMPPZ2014]The Captain

BZOJ4152: [AMPPZ2014]The Captain

BZOJ4152The Captain[DIjkstra]

bzoj4143: [AMPPZ2014]The Lawyer