P3385 WA on #1, #7, #8, #10 求助
//Bellman-Ford
#include<bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f
#define maxn 12345
using namespace std;
struct edge{
int u,v,w;
}e[maxn];
int n,m,s,t,dis[maxn],ans;
void BellmanFord(){
dis[s]=0;
for(int t=1;t<n;t++){
for(int i=1;i<=m;i++){
if(e[i].w>=0){
if(dis[e[i].u]<inf)dis[e[i].v]=min(dis[e[i].v],dis[e[i].u]+e[i].w);
if(dis[e[i].v]<inf)dis[e[i].u]=min(dis[e[i].u],dis[e[i].v]+e[i].w);
}
else
if(dis[e[i].u]<inf)dis[e[i].v]=min(dis[e[i].v],dis[e[i].u]+e[i].w);
}
}
}
signed main(){
cin>>t;
while(t--){
cin>>n>>m;
s=1;
for(int i=1;i<=m;i++)scanf("%lld%lld%lld",&e[i].u,&e[i].v,&e[i].w);
for(int i=1;i<=n;i++)dis[i]=inf;
BellmanFord();
if(dis[1]<0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}