Test Instructions: give a direction graph, the beginning S to the end of the T-K short, the absence of the output-1
#include <stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f;Const intMAXN =1024x768;Const intMAXM =100008;structedge{intV, NXT, W;};structnode{intPos, cost, F; BOOL operator< (ConstNODE & RHS)Const{//pay attention to symbols when overloaded if( This->f = = RHS. Freturn This->cost >RHS. cost; return This->f >RHS. F };}; EDGE EDGE[MAXM]; EDGE REDGE[MAXM];intHEAD[MAXN], RHEAD[MAXN];intcnt, rcnt;intN;voidinit () {memset (Head,-1,sizeof(Head)); memset (Rhead,-1,sizeof(Rhead)); CNT= RCNT =0;}voidAddedge (int from,intTo,intweight) {EDGE[CNT].W=weight; EDGE[CNT].V=to ; EDGE[CNT].NXT= head[ from]; head[ from] = cnt++;}voidAddredge (int from,intTo,intweight) {REDGE[RCNT].W=weight; REDGE[RCNT].V=to ; REDGE[RCNT].NXT= rhead[ from]; rhead[ from] = rcnt++;}intVIS[MAXN];intH[MAXN];voidSPFA (intSt) {Queue<int>que; memset (H, INF,sizeof(H)); memset (Vis,0,sizeof(VIS)); H[ST]=0; Que.push (ST); while(!Que.empty ()) { intCur =Que.front (); Que.pop (); Vis[cur]=0; for(intI=rhead[cur]; i!=-1; I=redge[i].nxt) { intv =redge[i].v; if(H[v] > H[cur] +REDGE[I].W) {H[v]= H[cur] +REDGE[I].W; if(!Vis[v]) {Vis[v]=1; Que.push (v); } } } }}intA_star (intSintTintk) { if(s = = t) k++; if(H[s]==inf)return-1; Priority_queue<NODE>que; NODE cur, into; Cur.pos=s; Cur. Cost=0; Cur. F=H[s]; Que.push (cur); intCNT =0; while(!Que.empty ()) {cur=Que.top (); Que.pop (); if(Cur.pos = = t) cnt++; if(CNT = = k)returncur. cost; for(intI=head[cur.pos]; i!=-1; I=edge[i].nxt) {into. Cost= cur. cost+EDGE[I].W; Into. F= cur. cost+edge[i].w+H[EDGE[I].V]; Into.pos=edge[i].v; Que.push (into); } }return-1;}intMainvoid){ intM, K, S, des; while(~SCANF ("%d%d", &n, &M)) {init (); int from, to, weight; while(m--) {scanf (" %d%d%d",& from, &to, &weight); Addedge ( from, to, weight); Addredge (To, from, weight);//Building a reverse edge} scanf (" %d%d%d", &s, &des, &j); SPFA (DES);//Find the shortest path to the end point, as H valueprintf"%d\n", A_star (s,des,k)); } return 0;}
View Code
First ==> what is a * pathfinding algorithm
The following reprint from ==> http://www.cnblogs.com/n-u-l-l/archive/2012/07/29/2614194.html
in a power map, the shortest path from the starting point to the end point becomes a short circuit, the 2nd Short road becomes the second short circuit, the 3rd Short road becomes the 3rd short circuit, and so on, the first K short road becomes the K short circuit. So, what about the K-short? for the K-short, can be thought of a relatively simple algorithm is the breadth of first search, using the priority queue from the source point S for wide search, when the K-time search to the end of T, the length of the request but this method in the operation of the process will produce a very large number of states, when the diagram is relatively simple, k comparison You can try it, but when K is larger or the number of points in the graph is large, there is a danger of exploding the stack. At present, more algorithms are used to single source the shortest-circuit mate a *. A * is a more advanced approach in search, A * algorithm combines heuristic methods (this method uses the information given by the graph to make the decision dynamically and makes the number of searches much lower) and the formal method (this method does not use the information given by the graph, but only through the mathematical form of analysis, such as the Dijkstra algorithm). It estimates the distance from the current point p to the end of the graph by an estimate function f (h), and thus determines its search direction, and when this path fails, it tries other paths. For a *, the valuation function = Current value + The distance from the current position to the end point, that is, F (P) =g (P) +h (p), which expands the value of the valuation function by one of the lowest values each time. For the K-short-circuit algorithm, G (p) is the path length from the source point S to the current point P, and H (p) is the shortest line from the current point p to the endpoint T, so the meaning of F (p) is the total distance from S to reach t after the current path through P points. That is, each extension has a direction, so it is good to increase the speed of the solution or to reduce the number of extended states. To speed up calculations, H (p) needs to be pre-preprocessed before searching, as long as all edges of the original image are reversed, and the single source is shorted from the end point T to get H (p). Single source shortest circuit to find the method has DIJKSTRA,BELLMAN-FORD,SPFA and so on.
Specific steps:
Here we use the chain forward to the star to store, due to the need to preprocess all points to the end of the shortest, you need to get all the edges of the graph G ', and then from the end point T do once single source the shortest, so in fact, two pictures.
(1) The direction of all the edges of the graph is reversed (no direction graph can omit this step), with the original end point T as the source point to do the single source shortest, the result is recorded in the array Dis[i], Dis[i] is the original midpoint I point t of the shortest distance. The dis[i here] is the H (p) above;
(2) Create a new priority queue and add the source point s to the queue;
(3) The lowest point P (p) is ejected from the priority queue, where F (p) equals points are present, G (p) the smallest point), if the point P is the end point T, then the number of times the t out of the queue, if the current is the K of T out of the team, then the current path length is s to T K Short, the algorithm ends Otherwise, all edges connected to P are traversed, and the extension-to-p adjacency Point information is added to the priority queue.
It is worth noting that when the s==t need to calculate (k+1) short-circuit, because s to t this distance of 0 of the road can not be counted in this K short circuit, then only the K self-increment 1 after the K-short can be.
POJ 2449 remmarguts ' Date (k-Short && * algorithm)