K short circuit (A * + SPFA)

 

 

 

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<queue>
  6 using namespace std;
  7 const int MAX_V = 1100;
  8 const int MAX_E = 110000;
  9 const int INF = 0x3f3f3f3f;
 10 
 11 struct ENode
 12 {
 13     int to;
 14     int w;
 15     int Next;
 16 };
 17 ENode edegs[MAX_E];
 18 ENode edegs1[MAX_E]; //存反图;
 19 int Head[MAX_V], Head1[MAX_V], tnt, tnt1;
 20 void Add_ENode (int a, int b, int w)
 21 {
 22     ++ tnt;
 23     edegs[tnt].to= b;
 24     edegs[tnt].w= w;
 25     edegs[tnt].Next= Head[a];
 26     Head[a]= tnt;
 27 //    ++ tnt;  //无向图
 28 //    edegs[tnt].to= a;
 29 //    edegs[tnt].w= w;
 30 //    edegs[tnt].Next= Head[b];
 31 //    Head[b]= tnt;
 32 }
 33 void Add_ENode1 (int a, int b, int w)
 34 {
 35     /**建反向图*/
 36     ++ tnt1;
 37     edegs1[tnt1].to= b;
 38     edegs1[tnt1].w= w;
 39     edegs1[tnt1].Next= Head1[a];
40      Head1 [A] = TNT1;
 41 is  //     ++ TNT1;   // undirected graph
 42 is  //     edegs1 [TNT1] .to = A;
 43 is  //     edegs1 [TNT1] .W = W;
 44 is  //     edegs1 [TNT1 ] = .NEXT Head1 [B];
 45  //     Head1 [B] = TNT1; 
46 is  }
 47  
48  int Dis [max_v];
 49  BOOL Visit [max_v]; // whether traversed; 
50  int que [max_v]; // analog queue; 
51 is  int outque [max_v]; // record the number of times at each point in the queue, preventing negative side, to ensure that no negative side may be omitted in FIG.; 
52 BOOL the SPFA ( int S, int n-)
 53 is  {
 54 is      / * * reverse the SPFA FIG processing, a preprocessing Dis [] * / 
55      / * S: start point; n: total number of points * / 
56 is      int IQ; // queue the number of elements; 
57 is      Memset (Dis, INF, the sizeof (Dis));
 58      Memset (Visit, to false , the sizeof (Visit));
 59      Memset (outque, 0 , the sizeof (outque));
 60      IQ = 0 ;
 61 is      que [IQ ++] = S;
 62 is     Visit [S] = to true ;
 63 is      Dis [S] = 0 ;
 64      int I = 0 ;
 65      the while (I < IQ)
 66      {
 67          int U = que [I];
 68          Visit [U] = to false ;
 69          outque [ U] ++ ;
 70          iF (outque [U]> n) return  to false ; // when a queue exceeds point out n times, there must exist a negative edge; 
71 is  
72          for ( int K = Head1 [U]; K =! - . 1 ; K = edegs1[k].Next)
 73         {
 74             /*bfs,和Dijkstra相似*/
 75             int v= edegs1[k].to;
 76             if (Dis[v]> Dis[u]+ edegs1[k].w)
 77             {
 78                 Dis[v]= Dis[u]+ edegs1[k].w;
 79                 if (! visit[v])
 80                 {
 81                     visit[v]= true;
 82                     que[iq ++]= v;
 83                 }
 84             }
 85         }
86          I ++ ;
 87      }
 88      return  to true ;
 89  }
 90  
91 is  struct the Node
 92  {
 93      int  from ;
 94      int G; // G represents the path length to the current point 
95      int F; // F = G + represents DIS DIS current point to the end of the shortest path, i.e., before pre-treatment 
96      BOOL  operator <( const the Node R & lt &) const 
97      {
 98          IF (RF == F) return RG- G < 0 ;
 99         return r.f- f< 0;
100     }
101 };
102 int A_star (int s, int t, int n, int k)
103 {
104     Node e, ne;
105     int cnt= 0;
106     priority_queue<Node> que;
107     if (s== t) k ++;
108     if (Dis[s]== INF) return -1;
109     e.from= s;
110     e.g= 0;
111     e.f= e.g+ Dis[e.from];
112     que.push(e);
113     while (! que.empty())
114     {
115         e= que.top();
116         que.pop();
117         if (e.from== t) cnt ++;
118         if (cnt== k) return e.g;
119         for (int i= Head[e.from]; i!= -1; i= edegs[i].Next)
120         {
121             ne.from= edegs[i].to;
122             ne.g= e.g+ edegs[i].w;
123             ne.f= ne.g+ Dis[ne.from];
124             que.push(ne);
125         }
126     }
127     return -1;
128 }
129 
130 void into()
131 {
132     /**初始化*/
133     memset(Head,-1,sizeof(Head));
134     memset(Head1,-1,sizeof(Head1));
135     tnt= -1; tnt1= -1;
136 }
137 
138 int main()
139 {
140     int n, m, k;
141     int a, b, w;
142     while (~ scanf("%d %d %d", &n, &m, &k))
143     {
144         into();
145         for (int i= 0; i< m; i ++)
146         {
147             scanf("% D% D% D " , & A, & B, & W);
 148              Add_ENode (A, B, W);
 149              Add_ENode1 (B, A, W);   // build trans FIG. 
150          }
 151          the SPFA (n-, n-) ; // process the shortest path from the end point to the other point distance; 
152          int FAR A_star = ( . 1 , n-, n-, K);
 153          the printf ( " % D \ n- " , FAR);
 154      }
 155      return  0 ;
 156 }
View Code

 

Guess you like

Origin www.cnblogs.com/Amaris-diana/p/11205523.html