The prefix and the relationship are processed first, and then it is obvious that this is a differential constraint. So that's the shortest-circuit problem.
By the way to review the SPFA plus SLF optimization is how to write, also learned to another stl--deque two-way queue.
#include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm > #include <fstream> #include <iostream> #include <deque> #define REP (i, L, R) for (int i = l; I <= R; i++) #define DOWN (i, L, R) for (int i = l; I >= R; i--) #define N 123#define M 2345#define ll long long using namespace s Td;inline int read () {int x=0, f=1; char Ch=getchar (); while (ch< ' 0 ' | | ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();} while (ch>= ' 0 ' && ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int y, z, N;} E[m]; int fir[n], en;int T, N, M, D[n], c[n], X, Y, Z;bool b[n], ans;deque <int> q;void adde (int x, int y, int z) {en++, e[ En].y = y, e[en].z = z, E[EN].N = fir[x], fir[x] = en;en++, e[en].y = x, e[en].z =-Z, E[EN].N = Fir[y], fir[y] = en;} void Init () {Rep (i, 1, en) e[i] = E[0];rep (i, 0, n) fir[i] = C[i] = 0; en = 0;ans = true;} int main () {t=read (); while (t--) {Init (); N=read (); M=read (); Rep (I, 1, m) x=reAD (), Y=read (), Z=read (), Adde (x-1, y, z); Rep (i, 0, N) d[i]=0, b[i]=1, C[i]=1, Q.push_back (i); while (!q.empty ()) {int x = q . Front (); Q.pop_front (), b[x]=0;if (c[x]>n+1) {ans=false; break;} int o = fir[x], y = E[o].y;while (o) {if (D[y] > D[x]+e[o].z) {d[y] = d[x]+e[o].z; if (!b[y]) b[y]=1, c[y]++,!q.empty () &&d[q.front ()]>d[y]? Q.push_front (y): Q.push_back (y);} O=E[O].N, Y=e[o].y;}} if (ans) printf ("true\n"); else printf ("false\n");} return 0;}
BZOJ-1202 cunning businessman.