求助大佬:为什么数组开5e3+10会TLE 而不是RE
查看原帖
求助大佬:为什么数组开5e3+10会TLE 而不是RE
1033360
retamian楼主2023/10/2 09:56

RT 求解惑

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e4 + 10;
int h[N], ne[N], e[N], w[N], idx=1;
int dis[N], cnt[N];
int n, m;
bool st[N];
queue <int> q;
void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa() {
	memset(dis, 0x3f, sizeof dis);
	dis[0]=0;
	st[0]=1;
	q.push(0);
	while(q.size()){
		int t=q.front();
		q.pop();
		st[t]=0;
		for(int i=h[t];~i;i=ne[i]){
			int j=e[i];
				if(dis[j]>dis[t]+w[i]){
					dis[j]=dis[t]+w[i];
					if(!st[j]){
					q.push(j);
					st[j]=1;
					cnt[j]++;
					if(cnt[j]>n) return 1;
				}
			}
		}
	}
	return 0;
}
int main() {
	cin >> n >> m;
	memset(h, -1, sizeof h);
	for (int i = 1; i <= m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		add(b, a, c);
	}
	for (int i = 1; i <= m; i++) {
		add(0, i, 0);
	}
	if (spfa()) cout << "NO";
	else {
		for (int i = 1; i <= n; i++) {
			cout << dis[i] << " ";
		}
	}
	return 0;
}
2023/10/2 09:56
加载中...