#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e3 + 10;
const int MOD = 1000000007;
const int INF = 1e9+10;
int n,m,s,cnt = 0;
int to[N],nxt[N],val[N],head[N];
void biuld_edge(int x,int y,int w)
{
nxt[++cnt] = head[x];
head[x] = cnt;
val[cnt] = w;
to[cnt] = y;
if(w>=0){
nxt[++cnt] = head[y];
head[y] = cnt;
val[cnt] = w;
to[cnt] = x;
}
}
int dis[N];
int vis[N];
bool op[N];
bool SPFA(){
for (int i = 1; i <=n; i++)
{
dis[i] = INF;
vis[i] = 0;
op[i] = false;
}
dis[1] = 0;
vis[1] = 1;
queue<int> ty;
ty.push(1);
while(!ty.empty()){
int x = ty.front();
op[x] = false;
ty.pop();
for (int i = head[x]; i!=-1; i = nxt[i])
{
if(dis[x]+val[i]<dis[to[i]]){
dis[to[i]] = dis[x] + val[i];
if(!op[to[i]]){
ty.push(to[i]);
op[to[i]] = true;
vis[to[i]]++;
if(vis[to[i]]>=n) return false;
}
}
}
}
return true;
}
void solve() {
memset(head,-1,sizeof(head));
cin>>n>>m;
for (int i = 1; i <=m; i++)
{
int x,y,w;
cin>>x>>y>>w;
biuld_edge(x,y,w);
}
if(SPFA()) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
return;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
开5e3 有几个点tle了开成5e4就过了?