subtask1 #2 WA求调
查看原帖
subtask1 #2 WA求调
1286553
doooge楼主2024/9/17 17:17
#include <bits/stdc++.h>
using namespace std;
struct ll
{
	int x,w;
};
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		vector <ll> v[5010];
		int num[5010] = {},dis[5010] = {},n,m;
		bool f[5010] = {},flag = true;
		cin >> n >> m;
		for(int i = 1 ; i <= m ; i++)
		{
			int x,y,w;
			cin >> x >> y >> w;
			v[x - 1].push_back({y , w});
			v[y].push_back({x - 1 , -w});
		}
		queue <int> q;
		for(int i = 1 ; i <= n ; i++)
		{
			dis[i] = 0;
			num[i] = 1;
			q.push(i);
		}
		while(!q.empty())
		{
			int tmp = q.front();
			q.pop();
			if(num[tmp] > n)
			{
				cout << "false" << endl;
				flag = false;
				break;
			}
			for(auto i : v[tmp])
			{
				if(dis[i.x] > dis[tmp] + i.w)
				{
					dis[i.x] = dis[tmp] + i.w;
					num[i.x] = num[tmp] + 1;
					q.push(i.x);
				}
			}
		}
		if(flag) cout << "true" << endl;
	}
	return 0;
}
2024/9/17 17:17
加载中...