Bzoj4152: [AMPPZ2014]The Captain
2018-03-28 13:20
447 查看
题面
传送门Sol
分别按XX轴,YY轴从小到大排序,相邻两个点建边权为ΔxΔx或ΔyΔy的边然后跑DijkstraDijkstra
# 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\n", dis ); return 0; }
相关文章推荐
- 【bzoj4152: [AMPPZ2014]The Captain】最短路
- Bzoj4152: [AMPPZ2014]The Captain
- BZOJ4152: [AMPPZ2014]The Captain
- BZOJ4152:[AMPPZ2014]The Captain——题解
- 4152: [AMPPZ2014]The Captain
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
- 4152: [AMPPZ2014]The Captain
- 4152: [AMPPZ2014]The Captain
- BZOJ 4152: [AMPPZ2014]The Captain 分层图最短路
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
- BZOJ 4152: [AMPPZ2014]The Captain 题解 【贪心】【SPFA】
- BZOJ 4152: [AMPPZ2014]The Captain(最短路)
- bzoj 4152: [AMPPZ2014]The Captain
- [BZOJ]4151: [AMPPZ2014]The Cave
- bzoj 4145: [AMPPZ2014]The Prices【状压dp】
- 4146: [AMPPZ2014]Divisors
- Bzoj 4147: [AMPPZ2014]Euclidean Nim(博弈)
- 【bzoj4152】【AMPPZ2014】The Captain
- bzoj 4145: [AMPPZ2014]The Prices 状压dp
- bzoj4152[AMPPZ2014] The Captain