POJ 3259 wormholes "Bellman_ford judgment negative ring"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.