92分,1WA
另外有个问题,数组为什么开1e4+10会RE和TLE啊,数据范围不是3e3+10吗
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,w[N],dist[N],h[N],e[N],ne[N],idx,cnt[N];
bool st[N];
void add(int a,int b,int c){
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
bool SPFA(){
memset(dist,0x3f,sizeof(dist));
dist[1]=0;
queue<int> q;
q.push(1);
st[1]=true;
while(!q.empty()){
int t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i]){
int j=e[i];
if(dist[j]>dist[t]+w[i]){
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n) return true;
if(!st[i]){
st[j]=true;
q.push(j);
}
}
}
}
return false;
}
void init(){
memset(h,-1,sizeof(h));
memset(e,0,sizeof(e));
memset(ne,0,sizeof(ne));
memset(w,0,sizeof(w));
memset(cnt,0,sizeof(cnt));
memset(st,0,sizeof(st));
}
int main(){
int T;
cin>>T;
while(T--){
init();
cin>>n>>m;
while(m--){
int x,y,z;
cin>>x>>y>>z;
if(z>=0){
add(x,y,z);
add(y,x,z);
}
else add(x,y,z);
}
cout<<(SPFA()?"YES":"NO")<<endl;
}
return 0;
}