• BZOJ 4152: [AMPPZ2014]The Captain


    4152: [AMPPZ2014]The Captain

    Time Limit: 20 Sec  Memory Limit: 256 MB

    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

    HINT

     

    Source

    考虑如何减少连边。

    对每一维进行分析:只需要连向它最近的一位的就可以了。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 #include<vector>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef long double ld;
    11 typedef pair<int,int> pr;
    12 const double pi=acos(-1);
    13 #define rep(i,a,n) for(int i=a;i<=n;i++)
    14 #define per(i,n,a) for(int i=n;i>=a;i--)
    15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
    16 #define clr(a) memset(a,0,sizeof(a))
    17 #define pb push_back
    18 #define mp make_pair
    19 #define fi first
    20 #define sc second
    21 #define pq priority_queue
    22 #define pqb priority_queue <int, vector<int>, less<int> >
    23 #define pqs priority_queue <int, vector<int>, greater<int> >
    24 #define vec vector
    25 ld eps=1e-9;
    26 ll pp=1000000007;
    27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
    28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
    29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
    30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
    31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
    32 ll read(){ ll ans=0; char last=' ',ch=getchar();
    33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
    34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    35 if(last=='-')ans=-ans; return ans;
    36 }
    37 const int M=800005,N=200005;
    38 int v[M],Next[M],head[N],vis[N],n,e,X[N],Y[N];
    39 ll dis[N],cost[M];
    40 struct nodeQ{
    41     ll v; int i; 
    42     friend bool operator <(nodeQ a,nodeQ b){
    43         return a.v>b.v;
    44     }
    45 };
    46 struct node{
    47     int x,y,i;
    48 }f[N];
    49 bool cmpx(node a,node b){
    50     return a.x<b.x;
    51 }
    52 bool cmpy(node a,node b){
    53     return a.y<b.y;
    54 }
    55 #include<queue>
    56 priority_queue<nodeQ> Q;
    57 void add(int x,int y){v[++e]=y; Next[e]=head[x]; head[x]=e; cost[e]=min(abs(X[x]-X[y]),abs(Y[x]-Y[y]));  }
    58 void Dij(){
    59     for (int i=1;i<=n;i++) dis[i]=1e17,vis[i]=0;
    60     dis[1]=0;
    61     Q.push((nodeQ){dis[1],1});
    62     while (!Q.empty()){
    63         int u=Q.top().i; Q.pop(); if (vis[u]) continue;
    64         vis[u]=1; 
    65         for (int i=head[u];i;i=Next[i]){
    66             if (dis[v[i]]>dis[u]+cost[i]){
    67                 dis[v[i]]=dis[u]+cost[i];
    68                 Q.push((nodeQ){dis[v[i]],v[i]});
    69             }
    70         }
    71     }
    72 }
    73 int main(){
    74     n=read();
    75     for (int i=1;i<=n;i++) f[i].x=X[i]=read(),f[i].y=Y[i]=read(),f[i].i=i;
    76     sort(f+1,f+n+1,cmpx);
    77     for (int i=1;i<n;i++) add(f[i].i,f[i+1].i),add(f[i+1].i,f[i].i);;
    78     sort(f+1,f+n+1,cmpy);
    79     for (int i=1;i<n;i++) add(f[i].i,f[i+1].i),add(f[i+1].i,f[i].i);
    80     Dij();
    81     printf("%lld",dis[n]);
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    记一次在黑盒环境下使用网络设备(华为)寻找主机
    mips交叉编译zlib
    Python 爬虫 之LOL皮肤图片抓取
    gitlab 新建项目 并将本地项目上传
    flask项目目录结构
    SQLALCHEM 初始化数据库
    python连接access数据库查询
    .Net Core 3.1 -- APISIX2.6 微服务网关入门
    SqlServer Agent代理无法启动(启动后自动关闭)的解决方法
    Centos7安装配置DNS服务器
  • 原文地址:https://www.cnblogs.com/SXia/p/7711579.html
Copyright © 2020-2023  润新知