BZOJ4152: [Amppz2014]the captain

Source: Internet
Author: User

Description The N points on a given plane, the cost of defining (X1,Y1) to (x2,y2) is min (|x1-x2|,|y1-y2|), which is the minimum cost to go from point 1th to point N.

The first line of input contains a positive integer n (2<=n<=200000) that represents the number of points. Next n rows, each row contains two integers x[i],y[i] (0<=x[i],y[i]<=10^9), which in turn represents the coordinates of each point.

Output an integer, which is the minimum cost.

Sample Input5
2 2
1 1
4 5
7 1
6 7Sample Output2
The distance from point test instructions I to J is min (|xi-xj|,|yi-yj|), we can convert it to two sides, a weight of |xi-xj|, a weight of |yi-yj|, which is obviously no problem. Then the problem is transformed into a one-dimensional problem, how to even the least side, we find that by the order of the dimensions of each adjacent points to connect the edge.
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i;i=next[i])using namespacestd;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) {        intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +l; }    return*head++;} InlineintRead () {intx=0, f=1;CharC=Getchar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=200010;Const intmaxm=800010;Const intinf=1e9;structDijkstra {intN,M,D[MAXN],DONE[MAXN],FIRST[MAXN],NEXT[MAXM]; structEdge {int  from, To,dist;}    EDGES[MAXM]; structHeapnode {intu,d; BOOL operator< (Constheapnode& ths)Const{returnD>THS.D;}    }; voidInitintN) { This->n=n;m=1; }    voidAddedge (intUintVintW) {edges[m]= (Edge) {u,v,w};next[m]=first[u];first[u]=m++; } priority_queue<HeapNode>Q; voidSolveints) {Rep (I,1, N) d[i]=inf; D[s]=0; Q.push ((Heapnode) {s,0});  while(!Q.empty ()) {            intx=q.top (). u;            Q.pop (); if(Done[x])Continue;d one[x]=1; ren {Edge& e=Edges[i]; if(d[e.to]>d[x]+e.dist) {d[e.to]=d[x]+e.dist;                Q.push ((Heapnode) {e.to,d[e.to]}); }}}}}sol;structPoint {intX,y,id;} A[MAXN];BOOLCMP1 (Point A,point b) {returna.x<b.x;}BOOLCMP2 (Point A,point b) {returna.y<b.y;}intMain () {intn=read (); Sol.init (n); Rep (I,1, n) a[a[i].id=i].x=read (), a[i].y=read (); Sort (A+1, a+n+1, CMP1); Rep (I,1, N) {        if(i!=1) Sol. Addedge (a[i].id,a[i-1].id,a[i].x-a[i-1].x); if(i!=n) Sol. Addedge (a[i].id,a[i+1].id,a[i+1].x-a[i].x); } sort (A+1, a+n+1, CMP2); Rep (I,1, N) {        if(i!=1) Sol. Addedge (a[i].id,a[i-1].id,a[i].y-a[i-1].y); if(i!=n) Sol. Addedge (a[i].id,a[i+1].id,a[i+1].y-a[i].y); } sol.solve (1);p rintf ("%d\n", Sol.d[n]); return 0;}
View Code

BZOJ4152: [Amppz2014]the captain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.