为啥这12号点过不了
查看原帖
为啥这12号点过不了
1139280
Eryi117楼主2024/5/14 20:35
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
#include<map>
#include <vector>
using namespace std;

#define ll long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define mm(a, b) memset(a, b, sizeof(a))
#define mp make_pair

typedef pair<int, int>pii;
const int inf = 0x3f3f3f3f;
const int N =1e6+10;

int n, m;
int e[N], ne[N], w[N], h[N], idx;
int dist[N], cnt[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool spfa()
{
	mm(dist,inf);
	mm(st,false);
	mm(cnt, 0);
	queue<int>q;
	q.push(1);
	st[1] = true;
	dist[1] = 0;
	while (q.size()) {
		int t = q.front();
		q.pop();
		st[t] = false;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (dist[j] > dist[t] + w[i]) {
				dist[j] = dist[t] + w[i];
				if (++cnt[j] > n)return true;
				if (!st[j]) {
					q.push(j);
					st[j] = true;
				}
			}
		}
	}
	return false;
}

int main() {
	ios;
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> m;
		mm(h, -1);
		for (int i = 1; i <= m; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			if (c >= 0) add(a, b, c), add(b, a, c);
			else add(a, b, c);
		}
		if (spfa()) cout << "YES" << "\n";
		else cout << "NO" << "\n";
	}
	
	return 0;
}
2024/5/14 20:35
加载中...