4152: [amppz2014]the captain Time limit:20 Sec Memory limit:256 MB
submit:700 solved:266
[Submit] [Status] [Discuss] 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
HINT Source
Acknowledgement Claris Upload
N2 must not be built .min Can ignore, build two sidesIf there is a third point between two points, then as long as the separate and the third point to build the edge, there is no need between the two points
- So each point only needs to go up and down to the nearest point of the edge of the connection, sorting can
Run Dijkstra
////main.cpp//Bzoj4152thecaptain////Created by Candy on 9/7/16.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intn=200005, inf=1e9+5; inlineintRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intN;structdata{intX,y,id;} P[n];BOOLcmpx (data a,data b) {returna.x<b.x;}BOOLcmpy (data a,data b) {returna.y<b.y;}structedge{intV,w,ne;} E[n*4];intH[n],cnt=0;voidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=CNT;}voidBuildgraph () {sort (P+1, p+1+n,cmpx); for(intI=1; i<=n-1; i++) Ins (p[i].id,p[i+1].id,p[i+1].x-p[i].x); Sort (P+1, p+1+n,cmpy); for(intI=1; i<=n-1; i++) Ins (p[i].id,p[i+1].id,p[i+1].y-p[i].y);}structhn{intu,d; BOOL operator< (ConstHN &RHS)Const{returnD>rhs.d;}};intD[n],done[n];p riority_queueQ;voidDijkstraints) { for(intI=1; i<=n;i++) d[i]=INF; D[s]=0; Q.push ((HN) {s,0}); while(!Q.empty ()) {HN x=q.top (); Q.pop (); intu=x.u; if(Done[u])Continue; Done[u]=1; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(d[v]>d[u]+E[I].W) {D[v]=d[u]+E[I].W; Q.push ((HN) {v,d[v]}); } } }}intMainintargcConst Char*argv[]) {N=read (); for(intI=1; i<=n;i++) P[i].x=read (), P[i].y=read (), p[i].id=i; Buildgraph (); Dijkstra (1); printf ("%d", D[n]); return 0;}
Bzoj4152the Captain[dijkstra]