# 【bzoj4152】[AMPPZ2014]The Captain 堆优化Dijkstra

5
2 2
1 1
4 5
7 1
6 7

2

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200010
using namespace std;
typedef pair<int , int> pr;
struct data
{
int x , y , id;
}a[N];
priority_queue<pr> q;
int head[N] , to[N << 2] , len[N << 2] , next[N << 2] , cnt , dis[N] , vis[N];
bool cmpx(data a , data b)
{
return a.x < b.x;
}
bool cmpy(data a , data b)
{
return a.y < b.y;
}
inline void add(int x , int y , int z)
{
to[++cnt] = y , len[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , len[cnt] = z , next[cnt] = head[y] , head[y] = cnt;
}
int main()
{
int n , i , x;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d%d" , &a[i].x , &a[i].y) , a[i].id = i;
sort(a + 1 , a + n + 1 , cmpx);
for(i = 1 ; i < n ; i ++ ) add(a[i].id , a[i + 1].id , a[i + 1].x - a[i].x);
sort(a + 1 , a + n + 1 , cmpy);
for(i = 1 ; i < n ; i ++ ) add(a[i].id , a[i + 1].id , a[i + 1].y - a[i].y);
memset(dis , 0x3f , sizeof(dis)) , dis[1] = 0 , q.push(pr(0 , 1));
while(!q.empty())
{
x = q.top().second , q.pop();
if(vis[x]) continue;
vis[x] = 1;
for(i = head[x] ; i ; i = next[i])
if(dis[to[i]] > dis[x] + len[i])
dis[to[i]] = dis[x] + len[i] , q.push(pr(-dis[to[i]] , to[i]));
}
printf("%d\n" , dis[n]);
return 0;
}


|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-10-19 14:43  GXZlegend  阅读(412)  评论(0编辑  收藏  举报