< learning notes > A * algorithm for K-short

Source: Internet
Author: User

A tunnel to the surface

A simple problem surface
        • The first line gives N (points), M (number of sides) (1 <= N <=, 0 <= M <= 100000).
        • The following M-line gives three number Ai,bi,ti per row, indicating a one-way edge with a weight of t from A to B.
        • Finally, the s,t,k is given, which indicates that the S-T short circuit is obtained.
        • Special:
          • S–>s (d=0) cannot be counted as a road at the same time as the beginning and the end.
          • The topic will give you multiple sets of data.
          • Even though there are several paths to t that are the same distances, they are still recorded as different paths.
Positive SolutionA * algorithm

G[i] for the distance from the beginning S to I, H[i] (expected) for the end point T to i the shortest, with SPFA pretreatment. Using the Dijkstra approach, start with S, and each time the smallest point of the current H[i]+g[i] is ejected from the heap. When the end-point T is ejected from the K-time, the g[i at this time] is the S-to-t-K short.

code QWQ
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7 using namespacestd;8 9 intn,m,a,b,c,s,t,k,cnt;Ten intfirst[1010],next[100010],h[1010]; One BOOLused[1010]; A  - structmaple{ -     intf,t,d; the}rode[100010]; -  - structleaf{ -     intf,g,h; +     BOOL operator< (ConstLeaf &a)Const{ -          returna.g+a.h<g+h;  +     } A };  at  - voidBuild (intFintTintd) - { -rode[++cnt]=(Maple) {f,t,d}; -next[cnt]=First[f]; -first[f]=CNT; in } -  to voidSPFA () + { -queue<int> q;//  thememset (H, the,sizeof(h)); *memset (Used,0,sizeof(used)); $h[t]=0;Panax Notoginsengused[t]=1; - Q.push (T); the      while(!q.empty ()) +     { A         intA=Q.front (); the Q.pop (); +used[a]=0; -          for(intI=first[a];i;i=Next[i]) $            if(h[rode[i].t]>h[a]+rode[i].d) $            { -h[rode[i].t]=h[a]+rode[i].d; -                   if(!used[rode[i].t]) the                   { -used[rode[i].t]=1;Wuyi Q.push (rode[i].t); the                   } -            }  Wu     } - } About  $ intSearch () - { -      if(h[s]>1e9+7)return-1;//from the beginning to the end -      if(s==t) + +K; APriority_queue<leaf> Q;//define the function without emptying the QWQ. +Q.push (leaf) {S,0, H[s]}); the       while(!q.empty ()) -      { $Leaf a=q.top (); the Q.pop (); the          if(a.f==T) the          { the--K; -               if(k==0)returnA.G; in          } the           for(intI=first[a.f];i;i=Next[i]) theQ.push (leaf) {rode[i].t,a.g+rode[i].d,h[rode[i].t]}); About      }  the      return-1;//I can't find the K-short . the } the intMain () + { -      while(SCANF ("%d%d", &n,&m)! =EOF) the     {BayiCnt=0; thememset (First,0,sizeof(first)); theMemset (Next,0,sizeof(next)); -           for(intI=1; i<=m;++i) -          {  thescanf"%d%d%d",&a,&b,&c); the Build (b,a,c); the          } thescanf"%d%d%d",&s,&t,&K);  - SPFA (); thememset (First,0,sizeof(first)); theMemset (Next,0,sizeof(next)); theCnt=0;94           for(intI=1; i<=m;++i) theBuild (RODE[I].T,RODE[I].F,RODE[I].D);//re-build the Edge the          intans=search (); theprintf"%d\n", ans);98     } About     return 0; -}

< Learning notes > A * algorithm for K-short circuits

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.