#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e3 + 10, M = 6e3 + 10;
int ne[M], e[M], h[N], w[M], idx;
int dis[N], cnt[N];
bool st[N];
int n, m;
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
bool spfa()
{
memset(dis, 0x3f, sizeof dis);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
queue<int> q;
q.push(1);
dis[1] = 0;
st[1] = true;
while (q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dis[j] > dis[t] + w[i])
{
dis[j] = dis[t] + w[i];
if (++cnt[t] >= n)
return false;
if (!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
return true;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
memset(h, -1, sizeof h);
idx = 0;
for (int i = 0; i < m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (c >= 0)
add(a, b, c), add(b, a, c);
else
add(a, b, c);
}
if (spfa())
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
1
3 5
1 2 -8
1 2 -7
1 2 -6
1 2 -5
2 3 4
输出YES,正解NO