标签:include mem blog 出错 size 禁忌 scan printf while
NO
(应该是无负环)#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=2e3+5,maxm=6e3+5;
int n,m,head[maxn],dis[maxn],cnt[maxn],cc;
struct Edge{int to,next,val;}e[maxm];
bool vis[maxn];
void A(int x,int y,int z){
e[++cc].to=y;
e[cc].next=head[x];
e[cc].val=z;
head[x]=cc;
}
void Init(){
cc=0;
memset(head,0,sizeof head);
memset(e,0,sizeof e);
memset(cnt,0,sizeof cnt);
memset(vis,0,sizeof vis);
}
bool spfa(int s){
for(int i=1;i<=n;++i)dis[i]=0x3f3f3f3f;
queue<int>q;
dis[s]=0;vis[s]=1;q.push(s);
while(!q.empty()){
int x=q.front();q.pop();vis[x]=0;
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
if(dis[y]>dis[x]+e[i].val){
dis[y]=dis[x]+e[i].val;
if(!vis[y]){
if(++cnt[y]>n){
return 1;
}
vis[y]=1;
q.push(y);
}
}
}
}
return 0;
}
int main(){
// freopen("1.in","r",stdin);
int t;scanf("%d",&t);
while(t--){
Init();
scanf("%d%d",&n,&m);
for(int i=1,x,y,z;i<=m;++i){
scanf("%d%d%d",&x,&y,&z);
if(z>=0)A(x,y,z),A(y,x,z);
else A(x,y,z);
}
if(spfa(1))printf("YES\n");
else printf("NO\n");
}
return 0;
}
正确答案:
NO
我用deque却跑出来是yes......
数据
标签:include mem blog 出错 size 禁忌 scan printf while
原文地址:https://www.cnblogs.com/Lour688/p/13280923.html