2022-10-28 14:15:17 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2021-07-03 02:36:57 +00:00
|
|
|
|
2022-10-28 14:15:17 +00:00
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
2021-07-03 02:36:57 +00:00
|
|
|
|
2022-10-28 14:15:17 +00:00
|
|
|
const int N = 1e5 + 5;
|
2021-07-18 14:01:45 +00:00
|
|
|
|
2022-10-28 14:15:17 +00:00
|
|
|
int n, m, s;
|
|
|
|
long long dist[N];
|
|
|
|
std::vector<std::pair<int, long long>> g[N];
|
|
|
|
bool vis[N];
|
|
|
|
|
|
|
|
void spfa() {
|
|
|
|
std::fill_n(dist, N, std::numeric_limits<int>::max());
|
|
|
|
|
|
|
|
std::queue<int> q;
|
|
|
|
|
|
|
|
q.emplace(s);
|
2021-07-18 14:01:45 +00:00
|
|
|
dist[s] = 0;
|
2022-10-28 14:15:17 +00:00
|
|
|
vis[s] = true;
|
|
|
|
|
2021-07-18 14:14:12 +00:00
|
|
|
while (!q.empty()) {
|
2022-10-28 14:15:17 +00:00
|
|
|
int u = q.front();
|
2021-07-18 14:14:12 +00:00
|
|
|
q.pop();
|
2022-10-28 14:15:17 +00:00
|
|
|
|
|
|
|
vis[u] = false;
|
|
|
|
|
|
|
|
for (auto e : g[u]) {
|
|
|
|
int v = e.first;
|
|
|
|
long long w = e.second;
|
|
|
|
|
|
|
|
if (dist[v] > dist[u] + w) {
|
|
|
|
dist[v] = dist[u] + w;
|
|
|
|
|
|
|
|
if (!vis[v]) {
|
|
|
|
vis[v] = true;
|
|
|
|
q.emplace(v);
|
|
|
|
}
|
2021-07-18 14:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 02:36:57 +00:00
|
|
|
|
|
|
|
int main() {
|
2022-10-28 14:15:17 +00:00
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
2021-07-18 14:01:45 +00:00
|
|
|
cin >> n >> m >> s;
|
2022-10-28 14:15:17 +00:00
|
|
|
|
|
|
|
for (int i = 1, u, v, w; i <= m; i++) {
|
2021-07-18 14:14:12 +00:00
|
|
|
cin >> u >> v >> w;
|
2022-10-28 14:15:17 +00:00
|
|
|
|
|
|
|
g[u].emplace_back(v, w);
|
2021-07-03 02:36:57 +00:00
|
|
|
}
|
2022-10-28 14:15:17 +00:00
|
|
|
|
|
|
|
spfa();
|
|
|
|
|
2021-07-03 02:36:57 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
2022-10-28 14:15:17 +00:00
|
|
|
cout << dist[i] << ' ';
|
2021-07-03 02:36:57 +00:00
|
|
|
}
|
2022-10-28 14:15:17 +00:00
|
|
|
|
2021-07-18 14:01:45 +00:00
|
|
|
cout << endl;
|
2022-10-28 14:15:17 +00:00
|
|
|
|
2021-07-03 02:36:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|