Home > Back-end >  SPFA most short-circuit negative ring
SPFA most short-circuit negative ring

Time:02-06

Can't help bosses, program
The first line of the input is an integer T, says the number of sets of test data, for each set of data format is as follows:

The first line has two integer, said the figure points respectively n and next article edge information of the given number m,

The next m lines, each line three integers, u, v, w,

If w acuity 0, it said there is a edge from u to v power for w, there is also a from v to u right for w while,
If w<0, the only says there is a edge from u to v power for w,
Code:
#include

#include

#include

#include

using namespace std;

Const int maxn=100000 + 5;

Int n, m, s;
Struct Edge
{
Int, v, w;
};

Vector G [maxn];
Int dis [maxn], [maxn CNT];
Bool inq [maxn], TMP;

Void SPFA (int s)
{
Queue Que.
Memset (CNT, 0, sizeof (CNT));
Memset (dis, 0 x3f, sizeof (dis));
Memset (inq, false, sizeof (inq));
Dis [s]=0;
Inq [s]=true;
Que. Push (s);
while(! Que. Empty ())
{
Int u=que. Front ();
Que. Pop ();
Inq [u]=false;
for(int i=0; I & lt; G [u]. The size (); I++)
{
Int [u] [I]. V=g, v, w=g [u] [I]. W.
If (dis [u] + w & lt; Dis [v])
{
Dis [v]=dis [u] + w;
if(! Inq [v]) que. Push (v);
If (+ + [v] CNT & gt; N)
{
TMP=1;
return;
}
}
}
}
}

Int main ()
{
Int t;
Cin> t;
While (t -)
{
Int I, u, v, w.
Cin> n> m;
While (m -)
{
Cin> U> V> w;
G [u] push_back ((Edge) {v, w});
If (w & gt; G=0) [v] push_back ((Edge)} {u, w);
}
For (I=1; I & lt;=n; I++)
{
If (TMP) break;
SPFA (I);
}
If (TMP) cout<& lt;" YES "& lt; The else cout<& lt;" NO "& lt; TMP=false;
}
return 0;
}
  • Related