POJ 2449 remmarguts ' Date (A * search for K short circuit)

Source: Internet
Author: User

Transmission Door
This is a bare k short-circuit problem, we will use a A * solution.
We can think of it like this when we design the valuation function H. Because H (n) <= h* (n) and as close as possible to h* (n), we think that we can find a shortest path from the target node to the rest of the node, which must be less than or equal to the actual value. Then a A * from the beginning to search, find a node V, so cnt[v] plus 1. When Cnt[v] > K can be pruned, because this must no longer k short circuit on the line. It's good to be proved by the counter-proof method. You can end the search when the target node is searched for the K-th time.
Note that this problem has a very pit, that is, if the beginning = The end point, then 0 is the shortest way (that is, K to + 1)

Code:

#include <cstdio>#include <queue>#include <cstring>using namespace STD;#define MAXM 100006#define MAXN 1006structnode{intV, W; Node *next;} edge[maxm<<1], *ADJ[MAXN], *mcnt = Edge, *RADJ[MAXN];intDIS[MAXN], S, E, K;BOOLINQ[MAXN];structD//data structure used to represent the search node{intG, H, V; D () {} d (intT1,intT2) {v = t1; g = t2; h = dis[v];}BOOL operator< (ConstD RHS)Const{returnG + H > rhs.g + rhs.h; }};voidAddedge (intUintVintW) {node *t = ++mcnt; T->v = v;    T->w = W;    T->next = Adj[u];    Adj[u] = t;    t = ++mcnt; T->v = u;    T->w = W;    T->next = Radj[v]; RADJ[V] = t;}voidSPFA (intU//SPFA used to preprocess the valuation function H{memset(Dis,0x3f,sizeofDIS);memset(INQ,0,sizeofINQ); Queue<int>Q Q.push (U); Dis[u] =0; Inq[u] =1; while(!q.empty ())        {u = Q.front (); Inq[u] =0; Q.pop (); for(Node *p = radj[u]; p; p = p->next)if(P->w + Dis[u] < Dis[p->v]) {Dis[p->v] = Dis[u] + p->w;if(!inq[p->v])                    {Q.push (P-&GT;V); INQ[P-&GT;V] =1; }            }    }}intCNT[MAXN];intAstar (intU//a* Search{priority_queue<d> q; Q.push (D (U,0)); while(!q.empty ())        {D tmp = Q.top ();        CNT[TMP.V] + +; Q.pop ();if(CNT[TMP.V] > K)Continue;//The pruning mentioned above        if(Cnt[e] = = K)returnTMP.G;//Find answers         for(Node *p = adj[tmp.v]; p; p = p->next) Q.push (D (p->v, TMP.G + p->w)); }return-1;}intM, N;intMain () {intT1, T2, T3;scanf("%d%d", &n, &m); for(inti =1; I <= m; i + +) {scanf("%d%d%d", &t1, &t2, &AMP;T3);    Addedge (T1, T2, T3); }scanf("%d%d%d", &s, &e, &k);if(S = = E)    K + +; SPFA (E);printf("%d\n", Astar (S));return 0;}

Copyright NOTICE: Please feel free to reprint O (∩_∩) o

POJ 2449 remmarguts ' Date (A * search for K short circuit)

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.