单源最短路60分求助
查看原帖
单源最短路60分求助
240668
计算机陈斌楼主2020/4/26 21:52
  cpp
  #include <bits/stdc++.h>

using namespace std;

const int N = 1010;

int n, m, s; 
int g[N][N], dist[N];
bool st[N];

void dijkstra() {
	memset(dist, 0x3f, sizeof(dist));
	dist[s] = 0;
	for (int i = 0; i < n; i++) {
		int t = -1;
		for (int j = 1; j <= n; j++)
			if (!st[j] && (t == -1 || dist[t] > dist[j])) t = j;
		st[t] = 1;
		for (int j = 1; j <= n; j++) 
				dist[j] = min(dist[j], dist[t] + g[t][j]); 
	}
}

int main() {
	cin >> n >> m >> s;
	memset(g, 0x3f, sizeof(g));
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a][b] = min(g[a][b], c);
		//g[b][a] = min(g[a][b], c);
	}
	dijkstra();
	for (int i = 1; i <= n; ++i) cout << dist[i] << " ";
	//cout << dist[n];
	return 0;
} 
2020/4/26 21:52
加载中...