BZOJ4152 [AMPPZ2014] The Captain [shortest]

You say that this simple question but I think for a long time, alas may still be too much food, tears, pulled down (sad) ┭┮﹏┭┮

Because of all this problem is a topic online solution, I can not understand the reason, is to ask the gods to hkk "It is clear that the practice of" rejected. . Obviously all the gods do question it?

Finally yy himself out a new approach should be called the new rigorous scientific understanding of the method, I do not know is not a correct idea

First, try to optimize the construction side, to a point, they even a $ \ min (\ Delta x, \ Delta y) $ edges, is there any way to go with some other side instead of the value?

To construct a grid graph if all points, the same meaning of the questions found $ X $ point moves on a vertical column is not easily spent, across adjacent columns have spent $ \ Delta x $, $ y $ empathy .

Thus, even an arbitrary side, $ \ $ min in the $ \ Delta x $ can be represented by a certain number of successive columns across each row takes a price. $ \ Delta y $ express across several lines every time it takes the sum of the cross.

In this way, the edges can be marked, and even then with a solution to a problem edge method prevailing, found that even $ 0 $ edges between peers or the same column, adjacent rows or columns as long as the election of a representative point even the edges, so take the shortest path.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define dbg(x) cerr << #x << " = " << x <<endl
 8 using namespace std;
 9 typedef long long ll;
10 typedef double db;
11 typedef pair<int,int> pii;
12 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
13 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
14 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
15 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
16 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
17 template<typename T>inline T read(T&x){
18     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
19     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
20 }
21 const int N=200000+7;
22 struct thxorz{int to,nxt,w;}G[N<<3];
23 int Head[N],tot;
24 inline void Addedge(int x,int y,int z){
25     G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot,G[tot].w=z;
26     G[++tot].to=x,G[tot].nxt=Head[y],Head[y]=tot,G[tot].w=z;
27 }
28 int n;
29 struct stothx{int x,y,id;}A[N];
30 inline bool cmp1(stothx a,stothx b){return a.x<b.x;}
31 inline bool cmp2(stothx a,stothx b){return a.y<b.y;}
32 priority_queue<pii,vector<pii>,greater<pii> > pq;
33 int dis[N];
34 #define y G[j].to
35 inline void dij(){
36     memset(dis,0x3f,sizeof dis);pq.push(make_pair(dis[1]=0,1));
37     while(!pq.empty()){
38         int x=pq.top().second,d=pq.top().first;pq.pop();
39         if(d^dis[x])continue;
40         if(x==n)return;
41         for(register int j=Head[x];j;j=G[j].nxt)if(MIN(dis[y],d+G[j].w))pq.push(make_pair(dis[y],y));
42     }
43 }
44 #undef y
45 int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
46     read(n);
47     for(register int i=1;i<=n;++i)read(A[i].x),read(A[i].y),A[i].id=i;
48     sort(A+1,A+n+1,cmp1);
49     for(register int i=1;i<n;++i)Addedge(A[i].id,A[i+1].id,A[i+1].x-A[i].x);
50     sort(A+1,A+n+1,cmp2);
51     for(register int i=1;i<n;++i)Addedge(A[i].id,A[i+1].id,A[i+1].y-A[i].y);
52     dij();
53     printf("%d\n",dis[n]);
54     return 0;
55 }
View Code

 

Guess you like

Origin www.cnblogs.com/saigyouji-yuyuko/p/11609822.html