Analysis: (official)
First of all consider violence and obviously can direct each o (n^2)
? of the side, and finally run a hierarchical chart the shortest way on the line.
Then we consider optimizing the process of the edge, because it is the operation of the interval, so it is very obvious to think of using line tree to maintain the entire graph,
Even when the corresponding interval is found, the nodes of the segment tree are connected to each other. This can greatly reduce the size of the edge, and then run a hierarchical map of the shortest way to it.
But in this way, every addition to the edge of the O (logn) segment tree nodes, although running very fast, but the complexity is still unscientific.
In order to solve the problem of the size of the edge, open two tree segment, you can create a new intermediate node, in the corresponding interval between the node and the middle node edge
Further reduced the size of the edge, forcibly descending by an order of magnitude
Finally, run a little bit, the shortest way on the hierarchy.
Complexity O (mlog^2n)
What are you going to do with the line tree but not the shortest way of layering? Amway JLOI2011 flight route.
Because the number of sides is still relatively more, so can not use SPFA, and to use Heap-dijkstra to do the shortest way,
But do not rule out that some serious players have a special SPFA posture can be done or ordinary SPFA write more graceful and accidentally ran over ...
Note: The question of the question is written in detail, and then JLOI2011 flight route is BZOJ2763 directly to do it.
Then my building is not perfect at first, ran the 600+ms, and then perfected a bit, according to the Line tree node map (this is the puzzle)
However, the node of each segment tree does not need to be connected with all the points in its area, just follow the structure of the line segment tree, and even the left and right son of it.
Code:
#include <stdio.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<string.h>using namespaceStd;typedefLong LongLL;Const intinf=0x3f3f3f3f;Const intn=5e4+1;intd[ One][n*Ten+ -],head[n*Ten+ -],tot,n,m,k;structedge{intV,w,next;}; Vector<Edge>Edge;voidAddintUintVintW) {Edge.push_back (Edge{v,w,head[u]}); Head[u]=edge.size ()-1;}structnode{intCur,v,dis; BOOL operator< (ConstNode &RHS)Const{ returnDis>Rhs.dis; }};p Riority_queue<Node>Q;BOOLvis[ One][n*Ten+ -];intDij () {memset (D,inf,sizeof(d)); memset (Vis,0,sizeof(VIS)); d[0][8*n+1]=0; Q.push (node{0,8*n+1,0}); while(!Q.empty ()) { intCur=q.top (). cur,u=q.top (). V; Q.pop (); if(Vis[cur][u])Continue; Vis[cur][u]=1; for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].v; if(!vis[cur][v]&&d[cur][v]>d[cur][u]+EDGE[I].W) {D[cur][v]=d[cur][u]+EDGE[I].W; Q.push (Node{cur,v,d[cur][v]}); } if(cur+1>K)Continue; if(!vis[cur+1][v]&&d[cur+1][v]>D[cur][u]) {D[cur+1][v]=D[cur][u]; Q.push (Node{cur+1, v,d[cur+1][v]}); } } } returnd[k][8*n+n]==inf?-1:d [k][8*n+n];}voidBuildintRtintLintR) { if(l==R) {Add (N*8+l,rt,0); Add (RT+4*n,n*8+l,0); return; } intMid= (l+r) >>1; Build (Rt<<1, L,mid); Build (Rt<<1|1, mid+1, R); Add (RT<<1Rt0), Add (rt<<1|1Rt0); Add (RT+4*n,4*n+rt*2,0), Add (rt+4*n,4*n+rt*2+1,0);}intnow,w;voidTREEADD1 (intRtintLintRintXinty) { if(x<=l&&r<=y) {Add (RT,NOW,W); return; } intMid= (l+r) >>1; if(X<=mid) TREEADD1 (rt<<1, L,mid,x,y); if(Y>mid) TREEADD1 (rt<<1|1, mid+1, r,x,y);}voidTREEADD2 (intRtintLintRintXinty) { if(x<=l&&r<=y) {Add (Now,rt+4*n,0); return; } intMid= (l+r) >>1; if(X<=mid) TREEADD2 (rt<<1, L,mid,x,y); if(Y>mid) TREEADD2 (rt<<1|1, mid+1, r,x,y);}intMain () {scanf ("%d%d%d%d",&n,&n,&m,&k); memset (Head,-1,sizeof(head)); Edge.clear (); Build (1,1, N); now=9*N; for(intI=1; i<=m;++i) { inta,b,c,d; scanf ("%d%d%d%d%d",&a,&b,&c,&d,&W); ++Now ; TREEADD1 (1,1, n,a,b); TREEADD2 (1,1, n,c,d); ++Now ; TREEADD1 (1,1, n,c,d); TREEADD2 (1,1, n,a,b); } intans=Dij (); if(ans==-1) printf ("Creationaugust is a sb!\n"); Elseprintf"%d\n", ans); return 0;}
View Code
HDU5669 Road Layered Shortest Path + segment tree Build map