Test instructions
Give a graph of n points m, and then give a starting point and an end point, the first to the end of the K short.
Ideas
A short circuit is obtained for K. A classic question.
spfa+a*
The core idea is based on the estimation function of a * search.
F (x) = g (x) + H (x)
Valuation function = s to x distance + x to t distance
The meaning of the valuation function is the distance through the path of the X point.
Each time we search, we select the value of the lower valued function to expand. So we search the state of the T point out of the order is, the shortest ... Short circuit .... Third Short circuit ...
It reduces the number of states we search.
Code implementation, implement a valuation function of the structure, and then the implementation of a priority queue. The distance from X to T is directly set up a reverse graph, run again SPFA, you can get so point to the end of the shortest.
Reference code:
/ * #pragma warning (disable:4786) #pragma comment (linker, "/stack:0x800000") */#include <cassert>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <sstream>#include <iomanip>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <algorithm>#include <iterator>#include <utility>using namespace STD;Template<classT > t _abs (t N) {return(N <0? -N:N);}Template<classT > t _max (t A, T b) {return(! (A < b)? A:B);}Template<classT > t _min (t A, T b) {return(A < b? a:b);}Template<classT > t sq (t x) {returnx * x;}Template<classT > t gcd (t A, T b) {return(b! =0? Gcd<t> (b, a%b): a);}Template<classT > t LCM (t A, T b) {return(A/gcd<t> (A, b) * b);}Template<classT >BOOLInside (t A, T B, t C) {returnA<=b && B<=c;}#define MIN (A, B) ((a) < (b)? (a): (b))#define MAX (A, B) ((a) > (b)? (a): (b))#define F (i, n) for (int (i) =0; (i) < (n); + + (i))#define REP (i, S, T) for (int (i) = (s);(i) <= (t); + + (i))#define UREP (i, S, T) for (int (i) = (s);(i) >= (t);--(i))#define REPOK (i, S, T, O) for (int (i) = (s);(i) <= (t) && (o); + + (i))#define MEM0 (addr) memset ((addr), 0, sizeof ((addr)))#define MP (x, y) make_pair (x, y)#define REV (S, e) reverse (S, e )#define SET (P) memset (pair,-1, sizeof (p))#define CLR (P) memset (p, 0, sizeof (p))#define MEM (P, v) memset (P, V, sizeof (p))#define CPY (d, s) memcpy (d, S, sizeof (s))#define READ (f) freopen (F, "R", stdin)#define WRITE (f) freopen (F, "w", stdout)#define SZ (c) (int) c.size ()#define PB (x) push_back (x)#define FF First#define SS Second#define LL Long Long#define LD long double#define PII pair< int, int >#define PSI pair< string, int >#define LS u << 1#define RS u << 1 | 1#define Lson L, Mid, U << 1#define Rson Mid, R, u << 1 | 1#define DEBUG (x) cout << #x << "=" << x << EndlConst intMAXN =1010;Const intMAXM =100010;Const intMaxnum =100010;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-10;Const intMoD =1e9;using namespace STD;structnode{intV,w,next;} EDGE[MAXM],REVEDGE[MAXM];structa{intF,g,v;BOOL operator< (ConstA &a)Const{if(A.F = = f)returnA.G < G;returnA.F < F; }};intvis[maxn],d[maxn],cnt;intHEAD[MAXN],REVHEAD[MAXN];intM,n,s,t,k;voidInit () {cnt =0; MEM (head,-1); MEM (revhead,-1);}voidAddedge (intXintYintT) {edge[cnt].v = y; EDGE[CNT].W = t; Edge[cnt].next = Head[x]; HEAD[X] = cnt; REVEDGE[CNT].V = x; REVEDGE[CNT].W = t; Revedge[cnt].next = Revhead[y]; Revhead[y] = cnt++;}voidSPFA (intSRC) {Rep (I,1, n) d[i] = INF; MEM0 (VIS); VIS[SRC] =0; D[SRC] =0; Queue<int>Que//while (!que.empty ()) Que.pop ();Que.push (SRC); while(!que.empty ()) {intU = Que.front (); Que.pop (); Vis[u] =0; for(inti = Revhead[u]; I! =-1; i = revedge[i].next) {intv = revedge[i].v;intW = REVEDGE[I].W;if(D[v] > D[u] + W) {D[v] = D[u] + W;if(!vis[v]) {Que.push (v); VIS[V] =1; } } } }}intSolveintSrcintDES) {priority_queue<a> q;intCNT =0;if(src = = des) k++;if(D[SRC] = = INF)return-1; A T,tt; T.V = src, t.g =0, T.f = T.g + D[SRC]; Q.push (t); while(!q.empty ()) {TT = Q.top (); Q.pop ();if(TT.V = = des) {cnt++;if(cnt = = k)returnTT.G; } for(inti = HEAD[TT.V]; I! =-1; i = edge[i].next) {T.V = EDGE[I].V; T.G = tt.g + edge[i].w; T.F = T.g + D[T.V]; Q.push (t); } }return-1;}intMain () {//read ("In.txt"); while(scanf("%d%d", &n,&m)!=eof) {init (); Rep (I,1, m) {intU,v,w;scanf("%d%d%d", &u,&v,&w); Addedge (U,V,W); }scanf("%d%d%d", &s,&t,&k); SPFA (t);//debug (d[2]); printf("%d\n", Solve (s,t)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2448 (k short Circuit, A*+SPFA) remmarguts ' Date