0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 18:56:28 +00:00

H1066. 【模板】单源最短路径

https://hydro.ac/record/61a187cd53a50c11fd2bd973
This commit is contained in:
Baoshuo Ren 2021-11-27 09:20:56 +08:00 committed by Baoshuo Ren
parent e4fca64680
commit fe05aa17ae
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, s, u, v, w;
long long dist[100005];
vector<pair<int, int>> g[100005];
bool st[100005];
void dijkstra() {
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
q.push(make_pair(0, s));
while (!q.empty()) {
auto t = q.top();
q.pop();
if (st[t.second]) continue;
for (auto i : g[t.second]) {
if (dist[i.first] > t.first + i.second) {
dist[i.first] = t.first + i.second;
q.push(make_pair(dist[i.first], i.first));
}
}
st[t.second] = true;
}
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
}
dijkstra();
for (int i = 1; i <= n; i++) {
cout << dist[i] << ' ';
}
cout << endl;
return 0;
}