40pts 代码求调 悬1关
查看原帖
40pts 代码求调 悬1关
780514
N1tr0us_Acid楼主2024/5/16 19:50

RT.

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 6e4 + 10;

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

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

int SPFA() {
	memset(dist, 0x3f, sizeof(dist));
	memset(st, 0, sizeof(st));
	memset(cnt, 0, sizeof(cnt));
	
	queue <int> q;
	
	q.push(1);
	st[1] = 1;
	
	while (!q.empty()) {
		int t = q.front();
		q.pop();
		
		st[t] = 0;
		
		for (int i = h[t]; i != -1; i = ne[i]) {
			int j = e[i];
			
			if(dist[j] > dist[t] + w[i]) {
				dist[j] = dist[t] + w[i];
				cnt[j] = cnt[t] + 1;
				
				if(cnt[j] > k) return 1;
				
				if(!st[j]) {
					q.push(j);
					st[j] = 1;
				}
			}
		}
	}
	
	return 0;
}

int main (void) {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> T;
	
	while (T --) {
		memset(h, -1, sizeof(h));
		memset(w, 0, sizeof(w));
		memset(e, 0, sizeof(e));
		memset(ne, 0, sizeof(ne));
		idx = 0;
		
		cin >> n >> m;
		k = m;
		
		for (int i = 1; i <= m; i ++) {
			int a, b, c;
			cin >> a >> b >> c;
			
			add(a, b, c);
			if(c >= 0) add(b, a, c), k ++;
		}
		
		int t = SPFA();
		
		if(t) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
		
	return 0;
}

2024/5/16 19:50
加载中...