Shortest circuit
Puzzle: http://zyfzyf.is-programmer.com/posts/97953.html
Sort by x-coordinate, with adjacent points connecting edges. Meet Dist (X1,X3) <=dist (x1,x2) +dist (x2,x3) (because you can walk y)
Sort by the y-coordinate, with the adjacent points connecting the edges. Ditto
But SPFA hung up ... Wrote Dijkstra.
1 /**************************************************************2 problem:41523 User:tunix4 language:c++5 result:accepted6 time:4304 Ms7 memory:27204 KB8 ****************************************************************/9 Ten //Bzoj 4152 One#include <vector> A#include <queue> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + using namespacestd; AtypedefLong LongLL; atInlineintGetint () { - intR=1, v=0;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') r=-1; - for(; isdigit (ch); Ch=getchar ()) v=v*Ten-'0'+ch; - returnr*v; - } in Const intn=2e5+Ten, inf=~0u>>1; - /*******************template********************/ to + inthead[n],to[n<<3],nxt[n<<3],l[n<<3],cnt; - voidAddintXintYintz) { theTo[++cnt]=y; NXT[CNT]=HEAD[X]; head[x]=cnt; l[cnt]=Z; *To[++cnt]=x; Nxt[cnt]=head[y]; head[y]=cnt; l[cnt]=Z; $ }Panax Notoginseng intN,d[n]; - structnode{ the intX,y,num; + }a[n]; A BOOLCMPX (node A,node b) {returna.x<b.x;} the BOOLCmpy (node A,node b) {returna.y<b.y;} +typedef pair<int,int>PII; - #defineMP Make_pair $ #defineSe Second $priority_queue<pii,vector<pii>,greater<pii> >Q; - BOOLVis[n]; - intMain () { the #ifndef Online_judge -Freopen ("4152.in","R", stdin);WuyiFreopen ("4152.out","W", stdout); the #endif -n=getint (); WuF (I,1, n) a[i].x=getint (), A[i].y=getint (), a[i].num=i; -Sort (A +1, a+n+1, cmpx); AboutF (I,1, N-1) Add (a[i].num,a[i+1].num,a[i+1].x-a[i].x); $Sort (A +1, a+n+1, cmpy); -F (I,1, N-1) Add (a[i].num,a[i+1].num,a[i+1].y-a[i].y); - -F (I,2, N) d[i]=INF; Ad[1]=0; +Q.push (MP (0,1)); the while(!Q.empty ()) { - intx=q.top (). SE; Q.pop (); $ if(Vis[x])Continue; thevis[x]=1; the for(intI=head[x];i;i=Nxt[i]) the if(d[to[i]]>d[x]+L[i]) { thed[to[i]]=d[x]+L[i]; - Q.push (MP (D[to[i]],to[i)); in } the } theprintf"%d\n", D[n]); About return 0; the}
View Code 4152: [amppz2014]the captain Time limit:20 Sec Memory limit:256 MB
submit:141 solved:62
[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
Hintsource
Acknowledgement Claris Upload
[Submit] [Status] [Discuss]
"Bzoj" "4152" "AMPZZ2014" the Captain