看 某个点的入队次数 times>=n
#include <bits/stdc++.h> using namespace std ; const int N=1e5+2,inf=0x7f7f3f; struct E{ int y,z; E(int y0,int z0){ y=y0,z=z0; } friend bool operator<(E x,E y){ return x.z>y.z; } }; vector<E> g[N]; queue<int> q; int n,m,dis[N],b[N],tms[N]; int spfa(int v0){ q.push(v0); int i,x,y,z; memset(dis,inf,sizeof(dis)); memset(b,0,sizeof(b)),memset(tms,0,sizeof(tms)); dis[v0]=0,b[v0]=1; tms[v0]++; while(!q.empty()){ x=q.front(); q.pop(); b[x]=0; for(i=0;i<g[x].size();i++){ y=g[x][i].y,z=g[x][i].z; if(dis[y]>dis[x]+z) { dis[y]=dis[x]+z; if(!b[y]){ b[y]=1,q.push(y); tms[y]++; if(tms[y]>=n) return 0; } } } } return 1; } int main(){ int i,T,x,y,z; cin>>T; while(T--){ cin>>n>>m; for(i=1;i<=n;i++) g[i].clear(); for(i=1;i<=m;i++){ cin>>x>>y>>z; g[x].push_back(E(y,z)); if(z>=0) g[y].push_back(E(x,z)); } while(!q.empty()) q.pop(); cout<<(spfa(1)?"NO":"YES")<<endl; } }
标签:tms,int,memset,v0,判负,push,dis From: https://www.cnblogs.com/towboa/p/17131722.html