为什么SPFA不让过
查看原帖
为什么SPFA不让过
459188
zrt090604楼主2022/11/25 20:10

这就是故意卡我SPFA的吗??? lgcmzz

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, s, idx, e[N], ne[N], w[N], h[N], dist[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++;
}
int spfa() {
	memset(dist, 0x3f, sizeof dist);
	dist[s] = 0;
	queue<int> q;
	q.push(s);
	st[s] = true;
	while(q.size()) {
		int t = q.front();
		q.pop();
		st[t] = false;
		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];
				if(!st[j]) {
					q.push(j);
					st[j] = true;
				}
			}
		}
	}
	if(dist[n] == 0x3f3f3f3f) return -1000000000;
	return dist[n];
}
int main () {
	scanf("%d%d%d", &n, &m, &s);
	memset(h, -1, sizeof h);
	int a, b, c;
	for(int i = 1;i <= m;++i) scanf("%d%d%d", &a, &b, &c), add(a, b, c);
	int t = spfa();
	if(t == -1000000000) printf("impossible"), exit(0);
	for(int i = 1;i <= n;++i) printf("%d ", dist[i]);
	return 0;
}
2022/11/25 20:10
加载中...