• Bzoj4152: [AMPPZ2014]The Captain


    题面

    传送门

    Sol

    分别按(X)轴,(Y)轴从小到大排序,相邻两个点建边权为(Delta x)(Delta y)的边
    然后跑(Dijkstra)

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(2e5 + 5);
    typedef int Arr[_];
     
    IL int Input(){
        RG int x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
     
    Arr first, dis, vis;
    int n, m, cnt;
    struct Point{
        int x, y, id;
    } p[_];
    struct Data{
        int u, dis;
     
        IL int operator <(RG Data B) const{
            return dis > B.dis;
        }
    };
    priority_queue <Data> Q;
    struct Edge{
        int to, next, w;
    } edge[_ << 2];
     
    IL void Add(RG int u, RG int v, RG int w){
        edge[cnt] = (Edge){v, first[u], w}, first[u] = cnt++;
        edge[cnt] = (Edge){u, first[v], w}, first[v] = cnt++;
    }
     
    IL int Cmp1(RG Point A, RG Point B){
        return A.x < B.x;
    }
     
    IL int Cmp2(RG Point A, RG Point B){
        return A.y < B.y;
    }
     
    IL void Dijkstra(){
        Fill(dis, 63), dis[1] = 0, Q.push((Data){1, 0});
        while(!Q.empty()){
            RG Data x = Q.top(); Q.pop();
            if(vis[x.u]) continue;
            vis[x.u] = 1;
            for(RG int e = first[x.u]; e != -1; e = edge[e].next){
                RG int v = edge[e].to, w = edge[e].w;
                if(dis[x.u] + w < dis[v]) Q.push((Data){v, dis[v] = dis[x.u] + w});
            }
        }
    }
     
    int main(RG int argc, RG char *argv[]){
        n = Input();
        for(RG int i = 1; i <= n; ++i)
            first[i] = -1, p[i] = (Point){Input(), Input(), i};
        sort(p + 1, p + n + 1, Cmp1);
        for(RG int i = 1; i < n; ++i)
            Add(p[i].id, p[i + 1].id, p[i + 1].x - p[i].x);
        sort(p + 1, p + n + 1, Cmp2);
        for(RG int i = 1; i < n; ++i)
            Add(p[i].id, p[i + 1].id, p[i + 1].y - p[i].y);
        Dijkstra();
        printf("%d
    ", dis[n]);
        return 0;
    }
    
  • 相关阅读:
    Java创建多线程的方法
    Spring Cloud 学习笔记 来自csdn
    Java线程退出
    Java线程的中断与插入
    Java守护线程
    Linux安装jdk
    内部类
    枚举,包类型
    jenkins
    设计模式之装饰者模式
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8663295.html
Copyright © 2020-2023  润新知