POJ 2449 (k short, A *)

Source: Internet
Author: User

Title: S-to-t short circuit.

Idea: Online is exclusively a * algorithm, so learning a bit. The so-called Astar algorithm is actually heuristic BFS. A valued function h is set here, combining the shortest short-circuit length of the current position with the estimate of the shortest path to the endpoint to select the next node to be expanded (the Dijkstra algorithm can be considered equal to the H value of all points, so the next expanded node is only relevant to the current minimum g). The value of this h is closer to the shortest way, but not less than the actual shortest path (otherwise it will be wrong), assuming that the H value is the actual short-circuit value, then the Astar algorithm can find a shortest, relative, if H is slightly larger than the actual value, then still can remove a lot of meaningless search volume. This problem requires k short-circuit, if not astar algorithm almost can't search ...

For this problem, we can take the H value equal to the actual value, which can be obtained by reversing the shortest path of the edge. Then still search from S, but the priority queue keyword to f=g+h, priority to f small point expansion. The first search must be the original shortest path point, wait until the original short-circuit search is finished, the minimum value of f in the queue will become larger, at this point may be removed from the previous expansion, but the search is a longer length of the other road. No matter how long it is, each extension will definitely expand the length of the road (to T) to search for a longer path, so there must be cnt[t]==k, when the length of the road extending to T is the length of the K-way. In addition, if s and T are not connected to the existence of the s can reach the circle, it is necessary to determine the number of expansion of each point more than K jumps out, otherwise it will die loop.

Ps: This question when s==t when k to add 1, probably because must walk only, because this WA for a long time = =

/** @author: cwind*http://www.cnblogs.com/Cw-trip/*/#include<iostream>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#include<functional>#include<Set>#definePB Push_back#defineFS First#defineSe Second#defineBK Back ()using namespaceStd;typedefLong LongLl;typedef pair<ll,ll>P;Const intmaxn=1020;intn,m;ints,t,k;structedge{intto,d; EDGE (intTo,intD): To, D (d) {}};vector<EDGE>G[maxn],rg[maxn];ll H[MAXN];intCNT[MAXN];voidinit () {memset (H,0x2f,sizeofh); H[t]=0; Priority_queue<P,vector<P>,greater<P> >Q; Q.push (P (0, T));  while(!Q.empty ()) {LL v=q.top (). se,d=q.top (). FS;        Q.pop (); if(h[v]<d)Continue;  for(intI=0; I<rg[v].size (); i++) {EDGE&e=Rg[v][i]; if(h[e.to]>h[v]+E.D) {H[e.to]=h[v]+E.D;            Q.push (P (h[e.to],e.to)); }        }    }}structa{ll F,g,p; A (ll f,ll g,ll P): F (f), G (g), p (p) {}BOOL operator< (ConstA &c)Const {        returnF>c.f; }};ll Astar () {priority_queue<A>Q; Q.push (A (h[s),0, S));  while(!Q.empty ()) {A STG=Q.top ();        Q.pop (); CNT[STG.P]++; if(stg.p==t&&cnt[t]==j) {            returnSTG.F; }        if(cnt[stg.p]>k)Continue;  for(intI=0; I<g[stg.p].size (); i++) {EDGE&e=G[stg.p][i]; Q.push (A (STG.G+e.d+h[e.to],stg.g+e.d,e.to)); }    }    return-1;}intMain () {Freopen ("/home/files/cppfiles/in","R", stdin); CIN>>n>>m;  for(intI=0; i<m;i++){        intA,b,c; scanf ("%d%d%d",&a,&b,&c);        G[A].PB (EDGE (b,c));    RG[B].PB (EDGE (a,c)); } CIN>>S>>T>>J;    Init (); if(s==t) k++; ll ans=Astar (); cout<<ans<<Endl; return 0;}
View Code

POJ 2449 (k short, A *)

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.