#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll INF = 2147483647;
constexpr int N = 1e5+1;
int n,m;
struct Node {
int v;
ll w;
};
vector<Node> e[N];
bitset<N> inq;
ll dis[N],val;
ll neg[N];
bool f = 0;
void spfa(int s) {
for(int i=1; i<=n; ++i) dis[i] = INF,inq[i] = 0,neg[i] = 0;
deque<int> q;
q.push_back(s);
inq[s] = 1;
neg[s] = 1;
dis[s] = 0;
while(!q.empty()) {
int u = q.front();
q.pop_front();
inq[u] = 0;
++neg[u];
if(neg[u] >= n) {
f = 1;
break;
}
for(const auto &x:e[u]) {
auto v = x;
if(dis[v.v] > dis[u] + v.w) {
dis[v.v] = dis[u] + v.w;
if(!inq[v.v]) {
inq[v.v] = 1;
if(!q.empty()&&dis[v.v] > dis[q.front()]) {
q.push_back(v.v);
} else q.push_front(v.v);
if(dis[q.front()] > dis[q.back()]) swap(q.front(),q.back());
}
}
}
}
if(f){
cout << "YES" << '\n';
}else cout << "NO" << '\n';
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin >> T;
while(T --) {
f = 0;
cin >> n >> m ;
for(int i=1;i<=n;++i) e[i].clear();
for(int i=1; i<=m; ++i) {
int x,y,w;
cin >> x >> y >> w;
if(w < 0){
e[x].push_back({y,w});
}else{
e[x].push_back({y,w});
e[y].push_back({x,w});
}
}
spfa(1);
}
return 0;
}
没看懂有什么问题