#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define FAST cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
const int N=1e4+10;
struct G
{
int nxt,to,edge;
}g[N];
int head[N],tot;
inline void add(int a,int b,int c)
{
g[++tot]=(G){head[a],b,c};
head[a]=tot;
}
int dist[N],c[N];
bool vis[N];
int n,m;
const int inf=1e9;
queue<int> q;
bool spfa(int s)
{
fill(dist,dist+N,inf);
q.push(s);
dist[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i;i=g[i].nxt)
{
int v=g[i].to;
if(dist[v]>dist[u]+g[i].edge)
{
dist[v]=dist[u]+g[i].edge;
c[v]=c[u]+1;
if(c[v]>n)
{
return 1;
}
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
}
int main()
{
FAST;
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
static int a,b,w;
cin>>a>>b>>w;
add(a,b,w);
}
if(spfa(1)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
spfa判负环,感觉没问题,样例是过的
求调