其他分享
首页 > 其他分享> > 【BZOJ4152】The Captain

【BZOJ4152】The Captain

作者:互联网

题面

http://darkbzoj.tk/problem/4152

题解

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#define ri register int
#define N 200050
#define LL long long
using namespace std;

vector<int> to[N],len[N];
int n,id[N];
struct node {int x,y;} a[N];
bool cmpx(int k1,int k2) {return a[k1].x>a[k2].x;}
bool cmpy(int k1,int k2) {return a[k1].y>a[k2].y;}

struct nod {
  int x; LL d;
  bool operator < (const nod &rhs) const {
    return d>rhs.d;
  }
};

LL dis[N];
bool vis[N];
priority_queue<nod> q;

void add_edge(int x,int y,int z) {
  to[x].push_back(y); len[x].push_back(z);
  to[y].push_back(x); len[y].push_back(z);
}

LL dij(){
  memset(dis,0x3f,sizeof(dis));
  dis[1]=0;
  q.push((nod){1,0});
  while (!q.empty()) {
    int x=q.top().x; q.pop();
    if (vis[x]) continue;
    vis[x]=1;
    for (ri i=0;i<to[x].size();i++) {
      int y=to[x][i];
      if (dis[y]>dis[x]+len[x][i]) {
        dis[y]=dis[x]+len[x][i];
        q.push((nod){y,dis[y]});
      }
    }
  }
  return dis[n];
}

int main(){
  scanf("%d",&n);
  for (ri i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
  for (ri i=1;i<=n;i++) id[i]=i;
  sort(id+1,id+n+1,cmpx);
  for (ri i=1;i<n;i++) add_edge(id[i],id[i+1],a[id[i]].x-a[id[i+1]].x);
  sort(id+1,id+n+1,cmpy);
  for (ri i=1;i<n;i++) add_edge(id[i],id[i+1],a[id[i]].y-a[id[i+1]].y);
  cout<<dij()<<endl;
}

 

标签:include,int,back,len,BZOJ4152,push,Captain,dis
来源: https://www.cnblogs.com/shxnb666/p/11278398.html