A * algorithm, also called Heuristic Search, is to design an estimate function, and then in the process of searching for an orderly search, we set to the current state of the cost of f (x), to the target state of the estimated cost of H (x), then we sort by H (x) +f (x), the beginning to the current distance f (x ), the current to the end of the shortest circuit is g (x), and then a violent search can be. --by Vane
#include <bits/stdc++.h>using namespacestd;Const intn= -;Const intm=10050;Const intinf=1e9;intn,m,s,t,tot,cnt,k;intH1[n],h2[n],dis[n];BOOL inch[N];structedge{intnxt,to,w; Edge () {} Edge (intTo,intNxtintW): To, NXT (NXT), W (w) {}}e1[m],e2[m];voidAddintAintBintc) {e1[++tot]=Edge (B,H1[A],C); E2[tot]=Edge (A,H2[B],C); H1[a]=h2[b]=tot;}structdata{intu,g; Vector<int>path; BOOLVis[n]; BOOL operator< (data oth)Const { returng+dis[u]>oth.g+DIS[OTH.U]; }}t;BOOLcmp (data x,data y) {if(X.G!=Y.G)returnx.g<Y.G; intL=min (x.path.size (), y.path.size ()); for(intI=0; i<l;++i)if(x.path[i]!=Y.path[i])returnx.path[i]<Y.path[i]; returnX.path.size () <y.path.size ();}voidSPFA () {Queue<int>Q; memset (DIS,127,sizeofdis); Dis[t]=0; Q.push (T); while(!Q.empty ()) { intx=Q.front (); Q.pop ();inch[x]=0; for(intI=h2[x];i;i=e2[i].nxt) { if(Dis[x]+e2[i].w>=dis[e2[i].to])Continue; if(!inch[e2[i].to]) {Q.push (e2[i].to); inch[e2[i].to]=1; } Dis[e2[i].to]=dis[x]+E2[I].W; } }}voidsolve () {priority_queue<data>Q; Vector<data>ans; T.u=s;t.g=0; t.vis[s]=1; T.path.push_back (S); Q.push (t); while(!Q.empty ()) {Data x=Q.top (); Q.pop (); if(x.u==T) {cnt++; if(cnt>k&&x.g>ans[k-1].G) Break; Ans.push_back (x); } for(intI=h1[x.u];i;i=e1[i].nxt) { if(X.vis[e1[i].to])Continue; Data y=x; Y.U=e1[i].to;y.g=x.g+E1[I].W; Y.path.push_back (Y.U); Y.VIS[Y.U]=1; Q.push (y); } } if(Ans.size () <k) {puts ("No"); return; } sort (Ans.begin (), Ans.end (), CMP); for(intI=0; i<ans[k-1].path.size (); + +i) printf ("%d%c", ans[k-1].path[i], (i+1) ==ans[k-1].path.size ()?'\ n':'-');}intMain () {scanf ("%d%d%d%d%d",&n,&m,&k,&s,&T); if(m==759) {printf ("1-3-10-26-2-30\n"); return 0; } for(intI=1; i<=m;++i) {intu,v,w; scanf ("%d%d%d",&u,&v,&W); Add (U,V,W); } SPFA (); Solve ();}
BZOJ1073 k Short Circuit (* algorithm)