[K short circuit] poj 2449

Source: Internet
Author: User

First, let's talk about a * algorithm. As we all know, the * algorithm is a heuristic search. The basic form is as follows:F (x) = g (x) + h (x); Where f (x) represents the total cost required at X, while G (x) represents: the actual cost from the source point to the X point. h (x) Represents the estimated cost from X to the end point.This function is an estimate. the actual cost from X to the end is H * (x). In the entire heuristic search, we must ensure that h (x) <= H * (X ); otherwise, the incorrect answer may be caused by an excessively high valuation value. The key to building a * Is to accurately plan an H (x) function so that it is close to H * (x). Such a search will make the Answer faster and more accurate. We can imagine that h (x) is too small to make the solution space too large, so that the search results will be accurate but slow, and the h (x) is too high to estimate, that is, the estimated cost is too high to make the results inaccurate.

In this way, we can understand the BFS search process. The estimated cost of h (x) is not taken into account in the BFS search process. That is to say, h (x) = 0, only g (x) is considered) the actual cost. In this way, you can search at the actual cost. Although it can be said that it is quite disgusting, we can also know that BFS has a lot of space to solve.

Write a * for the first time. Currently, it will only apply to K short circuits. But I also feel a little bit. The key lies in the design of h (x!

Describes how to use heuristic search to solve K short circuits.

First, we know the basic formula of a *: f (x) = g (x) + h (x); design h (x), according to the definition of h (x) the actual distance from X to T. That is to say, the distance between X-> T. Because many nodes are at the distance to T, to calculate this estimated value, we must first calculate the shortest path length of X-> T. Obviously, X has a lot of values, and T has only one value. It is not cost-effective to calculate the shortest path of a single source point for each X! Then, the shortest path of the single-Source Vertex that is sent from the T-point to the other vertex. In this case, evaluate the h (x) function and pay attention to the h (x) obtained in this way) = H * (X );

Then we can start heuristic search for the constructed h (x.

The first point is to define the head node. The cost of the head node is 0, the estimated cost is H [s], and the next point is V. Enter the queue and start the for loop. Each time the smallest node of f (x) in the team header is retrieved, other nodes are expanded. The number of expansion times for the current node is +. If the number of expansion times for the current node exceeds K, it obviously does not meet the requirements. If the number of extensions to the T node is exactly K, find the required number. The number of expansion times for the current node is the number of short circuits to the current node. Find the K short circuit of the node and return g (t), that is, the actual length consumed by K expansion.

In the for loop, all the sides of the current node can be expanded, and all the statuses are queued, the actual cost from the current node to the extended node is the actual cost of the current node + the edge length between two nodes. The next node is the expansion node, and the estimated function value is the distance from the expansion node to the target node h (x );

# Define n 1004 const int INF = (1 <30); int n, m; int dis [N]; // The shortest bool vis [N] from the current point to the end; struct node {int V; // The next vertex int DIS; // DISTANCE}; struct edge {int V; int W; friend bool operator <(edge a, edge B) {// reload the return. W + dis [. v]> B. W + dis [B. v] ;}}; vector <node> MP [N]; // forward neighbor table vector <node> remp [N]; // void Init () {int I; for (I = 0; I <= N; I ++) {MP [I]. clear (); remp [I]. clear () ;}// use spfa to find the shortest bool spfa (int s) to the end. {// s is Source Vertex number: queue <int> QQ; int I; for (I = 1; I <= N; ++ I) {dis [I] = inf; // set the distance from other points except the source point to infinity vis [I] = 0;} dis [s] = 0; // The distance from the source point is 0 vis [s] = 1; QQ. push (s); int U, V; while (! QQ. empty () {u = QQ. front (); QQ. pop (); vis [u] = 0; for (I = 0; I <remp [u]. size (); I ++) {node P = remp [u] [I]; If (DIS [p. v]> P. DIS + dis [u]) {dis [p. v] = P. DIS + dis [u]; If (! Vis [p. v]) {vis [p. v] = 1; QQ. push (P. v) ;}}}return true;} // K short circuit f (x) = g (x) + h (x) f (x) represents the total cost required at X, // G (x) represents the actual cost from the source point to the X point, h (x) int astar (int s, int T, int K) {// source point, target point, K short-circuit if (S = T) k ++; // when the source point is the same as the end point, k + 1 If (DIS [s] = inf) Return-1; // cannot reach the end point edge N1, N2; priority_queue <edge> PP; // priority queue. The minimum value of F int CNT [N] is obtained each time. memset (CNT, 0, sizeof (CNT )); // calculate K short circuit n1.v = s; n1.w = 0; pp. push (N1); While (! Pp. empty () {n1 = pp. top (); pp. pop (); CNT [n1.v] ++; int Len = n1.w; If (CNT [n1.v]> K) continue; If (CNT [T] = k) return Len; for (INT I = 0; I <MP [n1.v]. size (); I ++) {n2.v = MP [n1.v] [I]. v; // next point adjacent to N1 n2.w = MP [n1.v] [I]. DIS + Len; pp. push (N2) ;}} return-1 ;}int main () {While (scanf ("% d", & N, & M )! =-1) {int I, j; Init (); While (M --) {int U, V, W; scanf ("% d ", & U, & V, & W); node TMP; TMP. V = V; TMP. dis = W; MP [u]. push_back (TMP); TMP. V = u; remp [v]. push_back (TMP);} int S, T, K; scanf ("% d", & S, & T, & K); spfa (t ); printf ("% d \ n", astar (S, T, k);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.