10WA求助!!!
查看原帖
10WA求助!!!
660243
shenmeyebuhui楼主2023/5/3 13:34
#include<bits/stdc++.h>
using namespace std;
struct edge
{
	int to,cost;
};
vector<edge>G[2010];
int n,m,s,dist[2010],vis[2010],t,a,b,c,cnt[2010];
int spfa()
{
	queue<int>q;
	for(int i=1;i<=n;i++)
	{
		dist[i]=(1<<31)-1;
		vis[i]=0;
		cnt[i]=0;
	}
	q.push(s);
	dist[s]=0;
	vis[s]=1;
	cnt[1]=1;
	while(q.empty()==0)
	{
		int now=q.front();
		q.pop();
		vis[now]=0;
		for(int i=0;i<G[now].size();i++)
		{
			edge e=G[now][i];
			if(dist[e.to]>dist[now]+e.cost)
			{
				dist[e.to]=dist[now]+e.cost;
				if(vis[e.to]==0)
				{
					q.push(e.to);
					vis[e.to]=1;
					cnt[e.to]++;
					if(cnt[e.to]>=n)
					{
						return 1;
					}
				}
			}
		}
	}
	return 0;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>a>>b>>c;
			edge e;
			e.to=b;
			e.cost=c;
			G[a].push_back(e);
			if(c>=0)
			{
				e.to=a;
				e.cost=c;
				G[b].push_back(e);
		
			}
		}
		if(spfa()==1)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
		for(int i=1;i<=n;i++)
		{
			G[i].clear();
		}
	}
	return 0;
}

2023/5/3 13:34
加载中...