社区讨论

为啥会RE???

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

讨论操作

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

当前回复
3 条
当前快照
1 份
快照标识符
@mhj34qvb
此快照首次捕获于
2025/11/03 19:57
4 个月前
此快照最后确认于
2025/11/03 19:57
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n , m;
int sum[2010] , dis[2010];
bool vis[2010];
vector<int> G[2010] , W[2010];
queue<int> q;
bool spfa(int s)
{
	memset(dis , 0x3f , sizeof(dis));
	memset(vis , 0 , sizeof(vis));
	memset(sum , 0 , sizeof(sum));
	while(!q.empty()) q.pop();
	dis[s] = 0;
	vis[s] = 1;
	sum[s] = 1;
	q.push(s);
	while(!q.empty())
	{
		int x = q.front();
		q.pop();
		vis[x] = 0;
		for(int i = 0; i < G[x].size(); i++)
		{
			int y = G[x][i];
			if(dis[y] > dis[x] + W[x][i])
			{
				dis[y] = dis[x] + W[x][i];
				if(vis[y] == 0)
				{
					q.push(y);
					sum[y] ++;
					if(sum[y] >= n) return 1;
				}
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		cin >> n >> m;
		for(int i = 1; i <= n; i++)
		{
			G[i].clear();
			W[i].clear();
		}
		for(int i = 1; i <= m; i++)
		{
			int u , v , w;
			cin >> u >> v >> w;
			G[u].push_back(v);
			W[u].push_back(w);
			if(w >= 0)
			{
				G[v].push_back(u);
				W[v].push_back(w);
			}
		}
		if(spfa(1)) cout << "YES" << endl;
		else cout << "NO" << endl; 
	}
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...