还有这样ws卡bfs-spfa判负环的也是醉
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 #include<stack> 9 using namespace std; 10 #define Maxn 200010 11 12 struct node 13 { 14 int x,y,c,next; 15 }t[Maxn*2];int len; 16 int first[Maxn]; 17 18 void ins(int x,int y,int c) 19 { 20 t[++len].x=x;t[len].y=y;t[len].c=c; 21 t[len].next=first[x];first[x]=len; 22 } 23 24 int dis[Maxn]; 25 bool vis[Maxn],ok; 26 27 bool dfspfa(int x) 28 { 29 vis[x]=1; 30 for(int i=first[x];i;i=t[i].next) 31 { 32 int y=t[i].y; 33 if(dis[y]>dis[x]+t[i].c) 34 { 35 dis[y]=dis[x]+t[i].c; 36 if(vis[y]) 37 { 38 ok=1; 39 return 1; 40 } 41 if(dfspfa(y)) return 1; 42 } 43 } 44 vis[x]=0; 45 return 0; 46 } 47 48 int main() 49 { 50 int T; 51 scanf("%d",&T); 52 while(T--) 53 { 54 int n,m; 55 scanf("%d%d",&n,&m); 56 len=0; 57 memset(first,0,sizeof(first)); 58 for(int i=1;i<=m;i++) 59 { 60 int x,y,c; 61 scanf("%d%d%d",&x,&y,&c); 62 if(c>=0) ins(x,y,c),ins(y,x,c); 63 else ins(x,y,c); 64 } 65 ok=0; 66 memset(dis,0,sizeof(dis)); 67 memset(vis,0,sizeof(vis)); 68 for(int i=1;i<=n;i++) 69 { 70 dfspfa(i); 71 if(ok) {printf("YE5\n");break;} 72 } 73 if(!ok) printf("N0\n"); 74 } 75 return 0; 76 }
判负环
2016-11-18 11:06:25
时间: 2024-09-30 16:29:49