k short circuit solver:
k = shorted single source shortest road race reverse side Advanced Search + A *
A * algorithm is a combination of formal methods and heuristics;
Heuristic information given by leveraging FIG dynamically decide the number of searches is greatly reduced;
FIG formal method without using information given only by analyzing the mathematical form;
Algorithms ideas:
By one estimate algorithm function f (h) in FIG estimate the distance to the end of the current point p, and thus determine its search direction;
When this path fails, it tries a different path;
For A *, the current value of the evaluation function = current position + the distance to the end point, i.e., f (p) = g (p) + h (p), each extended a minimum evaluation function value;
For the shortest path algorithm is K, g (p) of the current path from s to p are moving length; H (p) is the point p to the length of the shortest of t;
Significance f (p) is then went from p s t in accordance with the current path to an end at least a total of how far to go;
In order to speed up the calculation, h (p) need to be pretreated prior to an A * search, from the end point t do a single source shortest path can be obtained for each point of h (p) a;
Algorithm steps:
(1) , the original end point t is the source point, solving t shortest distance of all points (i.e.: run again shortest );
(2) , create a priority queue , the source s is added to the queue;
(3) , the pop-f (p) from the priority queue in the minimum distance of the point p, the point p is if t, t is the number of teams is calculated;
If the current is t k-th dequeue length, the current path is s to t k-th short length , the algorithm terminates;
Otherwise traversing all edges connected to p will extend out to the adjacent point p information to the priority queue;
Code:
struct B { LL X, T; B (X LL, LL T): X (X), T (T) {} Friend BOOL operator < (B AA, BB B) { return aa.t DIS + [aa.x ]> bb.t + DIS [bb.x]; // priority queue, dis end point as a starting point for the pretreatment shortest } }; LL the ASTAR () // A * k-edge seeking article { IF (DIS [S] > = INF) return - . 1 ; The priority_queue <B> Q; B Re (S, 0 ); int NUM = 0 ; q.push (Re); the while (! q.empty ()) { re=q.top(); q.pop(); if(re.x==e) { num++; if(num==k)//找到第k条路 return re.t; } for(int i=pre2[re.x];~i;i=a2[i].next)//进队 { q.push(B(a2[i].y,a2[i].len+re.t)); } } return -1; }
Transfer from http://keyblog.cn/article-24.html