Test instructions: Give the n point, the edge of the positive right of the M bar, the side of the negative right of the W bar, ask if there is a negative ring
Because Bellman_ford most slack n-1 times, because from the beginning 1 end point n up to the n-2 point, that is, the most relaxation n-1 times, if the nth relaxation can be successful, it indicates that there is a negative ring
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intmaxn=10005; - intD[MAXN]; - intFlag; - intnodenum,edgenum,ecnt; + - structedge{ + intU,v,cost; A } E[MAXN]; at - intBellman_ford () { - for(intI=1; i<=nodenum;i++) d[i]=INF; -d[1]=0; - - for(intI=1; i<nodenum;i++){ in for(intj=1; j<=ecnt;j++){ - if(d[e[j].v]>d[e[j].u]+e[j].cost) tod[e[j].v]=d[e[j].u]+E[j].cost; + } - } the *flag=1; $ for(intI=1; i<=ecnt;i++){Panax Notoginseng if(d[e[i].v]>d[e[i].u]+e[i].cost) { -flag=0; the Break; + } A } the returnFlag; + } - $ voidAddedges (intAintBintc) { $e[++ecnt].u=A; -e[ecnt].v=b; -e[ecnt].cost=C; the } - Wuyi intMain () { the intm,w,ncase; - inta,b,c; Wuscanf"%d",&ncase); - while(ncase--){ AboutEcnt=0; $scanf" %d%d%d",&nodenum,&m,&W); - while(m--){ -scanf" %d%d%d",&a,&b,&c); - addedges (a,b,c); A addedges (B,A,C); + } the while(w--){ -scanf" %d%d%d",&a,&b,&c); $c=-C; the addedges (a,b,c); the } the if(Bellman_ford ()) printf ("no\n"); the Elseprintf"yes\n"); - } in return 0; the}View Code
POJ 3259 wormholes "Bellman_ford judgment negative ring"