社区讨论

求助大佬为什么本地过了评测不过

P3385【模板】负环参与者 3已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@mi7cq8o5
此快照首次捕获于
2025/11/20 19:32
4 个月前
此快照最后确认于
2025/11/20 19:32
4 个月前
查看原帖
RT,在本地和洛谷IDE上都测评通过,反而在评测中都WA,这是为什么
代码如下:
CPP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
using namespace std;

int d[10500],n,m,x,y,z,t,k,head[10500],cou[10500];
bool vis[10500];

struct c{
	int w,to,next;
}a[105000];

void add(int u,int v,int w)
{
	a[t].to=v;
	a[t].w=w;
	a[t].next=head[u];
	head[u]=t;
	t++;
}

bool spfa()
{
	queue <int> q;
	memset(vis,0,sizeof(vis));
	memset(d,88,sizeof(d));
	memset(cou,0,sizeof(cou));
	cou[1]=1;
	d[1]=0;
	vis[1]=1;
	q.push(1);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=a[i].next)
		{
			int v=a[i].to;
			if(d[v]>d[u]+a[i].w)
			{
				if(++cou[v]>=n)	return 1;
				d[v]=d[u]+a[i].w;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	return 0;
}

int main()
{
	freopen("1.txt","r",stdin);
	cin>>k;
	while(k--)
	{
		cin>>n>>m;
		t=1;
		memset(head,-1,sizeof(head));
		//memset(a,0,sizeof(a));
		for(int q=1;q<=m;q++)
		{
			scanf("%d%d%d",&x,&y,&z);
			if(z<0)
				add(x,y,z);
			else{
				add(x,y,z);
				add(y,x,z);
			}
		}
		if(k!=0)
		if(spfa())
			cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
		else
		if(spfa())
			cout<<"YES";
		else cout<<"NO";
	}
}
数据如下
CPP
10
9 16
1 2 -4
1 4 0
1 7 -4
1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