SPFA algorithm template (Liu Rujia Version) - Wormholes POJ - 3259

Purple always felt more refined the code book, did as incomplete on the purple book SPFA wrote an algorithm to determine whether there is a negative side of the question right, title links: https://vjudge.net/problem/POJ-3259 , look at the details Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include<iostream>
 6 #include<vector>
 7 #include<set>
 8 #include<queue>
 9 #define MAXN 500100
10 #define INF 0x3f3f3f3f
11 using namespace std;
12 typedef long long ll;
13 int n,m,w;
14 struct node
15 {
16     int from , to, cost;
 . 17      Node ( int A, int B, int C): from (A), to (B), cost (C) {}
 18 is }; /// structure edge 
. 19 Vector < int > G [MAXN]; /// G i representative elements starting from the edge [i] in 
20 is Vector <Node> edges; /// store all edges 
21 is  BOOL INQ [MAXN];
 22 is  int D [MAXN], CNT [MAXN];
 23 is  BOOL the SPFA ( int S)
 24  {
 25  
26 is      for ( int I = 0;i<=n;i++)d[i]=INF;
27     memset(inq,0,sizeof(inq));
28      memset(cnt,0,sizeof(cnt));///初始化
29     d[s]=0;
30     inq[s]=true;
31     queue<int> q;
32     q.push(s);
33     while(!q.empty())
34     {
35         int u=q.front();q.pop();
36         inq[u]=false;
37         for ( int I = 0 ; I <G [U] .size (); I ++) /// on the head of the queue as the starting point to edge relaxation 
38 is          {
 39              Node & E = Edges [G [U] [I]];
 40              IF (D [U] <INF && D [e.to]> D [U] + e.cost)
 41 is              {
 42 is                  D [e.to] = D [U] + e.cost;
 43 is                  IF (INQ [E! .to]) /// each queued relaxation point 
44 is                  {
 45                      q.push (e.to);
 46 is                      INQ [e.to] = to true ;
 47                      IF (CNT ++ [e.to]> n-) return to false ; /// If a slack point is n times the above-described negative ring 
48                  }
 49              }
 50          }
 51 is      }
 52 is      return  to true ;
 53 is  }
 54 is  void AddEdge ( int  from , int to, int dist)
 55      {
 56 is          edges.push_back (Node ( from , to, dist));
 57 is          int K = edges.size ();
 58          G [ from ] .push_back (- K- . 1 ); ///初始化边,将他们编号
59     }
60 int main()
61 {
62     int t;
63     scanf("%d",&t);
64     while(t--)
65     {
66         scanf("%d%d%d",&n,&m,&w);
67         for(int i=1;i<MAXN;i++)G[i].clear();
68         edges.clear();
69         for(int i=0;i<m;i++)
70          {
 71 is              int U, V, cost;
 72              Scanf ( " % D% D% D " , & U, & V, & cost);
 73 is              AddEdge (U, V, cost); AddEdge (V, U, cost); / // this path is bidirectional (I here cards long) 
74          }
 75          for ( int I = 0 ; I <W; I ++ )
 76          {
 77              int U, V, cost;
 78              Scanf ( " % D% D% D " , & U, V &, & cost);
 79              AddEdge (U, V, -cost); /// wormhole weight is negative, and one-way 
80         }
81         if(!SPFA(1))printf("YES\n");
82         else printf("NO\n");
83     }
84     return 0;
85 }

 

Guess you like

Origin www.cnblogs.com/megadeth/p/11318218.html