RT,除了最后一个其他的都wa了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
int t,n,m,head[2005],dis[2005],cnt[2005],tot;
struct edge{
int end,weith,next;
}e[6005];
void addedge(int x,int y,int w)
{
e[++tot].end=y;
e[tot].next=head[x];
e[tot].weith=w;
head[x]=tot;
}
bool spfa()
{
queue <int> q;
dis[1]=0;
cnt[1]=1;
q.push(1);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=head[now];i;i=e[i].next){
int y=e[i].end;
if(dis[y]>dis[now]+e[i].weith){
dis[y]=dis[now]+e[i].weith;
++cnt[y];
if(cnt[y]>n)
return false;
q.push(y);
}
}
}
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin>>t;
while(t--){
cin>>n>>m;
tot=0;
memset(head,0,sizeof(head));
memset(dis,INF,sizeof(dis));
memset(cnt,0,sizeof(cnt));
memset(e,0,sizeof(e));
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
addedge(u,v,w);//题解里面都是w>0的时候才调用addedge,但是我加了if(w>0)连样例都过不去
}
if(spfa())
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}