#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
 
#define MAXM 500010
#define MAXN 10010
#define ANS_MAX 2147483647
 
struct EDGE {
	int next;
	int to;
	int w;
};
EDGE edge[MAXM];
 
int n, m, st, cnt;
int head[MAXN];
 
int d[MAXN];
bool inq[MAXN];
 
inline int Read() {
	char c; int ans = 0; bool Sign = false;
	while(!isdigit(c=getchar()) && c != '-');
	if(c == '-') {
		Sign = true;
		c = getchar();
	}
	do {
		ans = (ans<<3) + (ans<<1) + (c ^ '0');
	} while(isdigit(c=getchar()));
	return Sign ? -ans : ans;
}
 
void Add(int u, int v, int w) {
	edge[++cnt].next = head[u];
	edge[cnt].to = v;
	edge[cnt].w = w;
	head[u] = cnt;
}
 
void read() {
	int x, y, w;
	n = Read();
	m = Read();
	st = Read();
	for(int i=1; i<=m; i++) {
		x = Read();
		y = Read();
		w = Read();
		Add(x, y, w);
	}
}
 
void SPFA(int x) {
	d[x] = 0; for(int i=1; i<=n; i++) d[i] = ANS_MAX;
	queue<int> Q; Q.push(x); inq[x] = true;
	while(!Q.empty()) {
		int k = Q.front(); Q.pop(); inq[k] = false;
		for(int i=head[k]; i!=0; i=edge[i].next) {
			int j = edge[i].to;
			if(d[j] > d[k] + edge[i].w) {
				d[j] = d[k] + edge[i].w;
				if(!inq[j]) {
					Q.push(j);
					inq[j] = true;
				}
			}
		}
	}
	for(int i=1; i<=n; i++) printf("%d ", d[i]);
	printf("\n");
}
 
int main() {
	read();
	SPFA(st);
	return 0;
}

https://blog.csdn.net/Binary_Heap/article/details/78209086

发表评论

邮箱地址不会被公开。 必填项已用*标注