First of all, this is a check-up constraint system ... (What's that? ←_←)
Then judge the feasible solution, the direct SPFA sentence negative circle.
BFS version SPFA to record the number of each point into the team, if the greater than the point represents a negative circle ... But good slow σ (° °| | |) ︴
Then use the DFS version of SPFA (thought it would be slow ...) , in fact, the speed is very fast orz)
1 /**************************************************************2 problem:34363 User:rausen4 language:c++5 result:accepted6 time:36 Ms7 memory:1236 KB8 ****************************************************************/9 Ten#include <cstdio> One A using namespacestd; - Const intN =10005; - Const intM =N; the Const intMaxLen = -*M; - - structEdges { - intNext, to, V; + edges () {} -Edgesint_n,int_t,int_v): Next (_n), to (_t), V (_v) {} + } E[m]; A at intN, M; - intfirst[n], tot; - intD[n]; - intV[n], flag; - CharBuf[maxlen], *c =buf; - intLen; in -InlineintRead () { to intx =0; + while(*c <'0'||'9'< *c) + +C; - while('0'<= *c && *c <='9') thex = x *Ten+ *c-'0', ++C; * returnx; $ }Panax Notoginseng -InlinevoidAdd_edge (intXintYintz) { theE[++tot] =edges (first[x], y, z); +FIRST[X] =tot; A } the + voidSPFA (intp) { - if(V[p]) { $Flag =1; $ return; - } - intx, y; theV[P] =1; - for(x = first[p]; x; x =e[x].next)Wuyi if(D[p] + e[x].v < D[y =E[x].to]) { theD[y] = D[p] +e[x].v; - SPFA (y); Wu if(flag)return; - } AboutV[P] =0; $ } - - intMain () { -Len = Fread (c,1, MaxLen, stdin); ABuf[len] =' /'; + intI, Oper, x, Y, Z; then = Read (), M =read (); - for(i =1; I <= m; ++i) { $Oper = Read (), x = Read (), y =read (); the if(Oper = =3) Add_edge (x, Y,0); the Else { thez =read (); the if(Oper = =1) Add_edge (x, Y,-z); - ElseAdd_edge (y, x, z); in } the } the for(i =1; I <= N; ++i) { About SPFA (i); the if(flag) Break; the } thePuts (flag?)"No":"Yes"); + return 0; -}
View Code
BZOJ3436 Little K's Farm