#include<bits/stdc++.h>
using namespace std;
#define L(i,j,k) for(int i=(j);i<=(k);i++)
#define R(i,j,k) for(int i=(j);i>=(k);i--)
const int M = 1e4 + 10;
const int N = 5e5+10;
vector<pair<int,int>> G[M];
int n, m, s, T, dist[N], vis[N];
void dijk(int s) {
L(i,1,n) dist[i] = pow(2,31)-1;
dist[s] = 0;
priority_queue<pair<int,int>> q;
q.push({0,s});
while(!q.empty()) {
int x = q.top().second;
q.pop();
if(vis[x]) continue;
vis[x] = 1;
for (pair<int,int> to : G[x]) {
if(dist[to.first] > dist[x] + to.second) {
dist[to.first] = dist[x] + to.second;
q.push({-dist[to.first], to.first});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> s;
L(i,1,m) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v,w});
}
dijk(s);
L(i,1,n) cout << dist[i] << " ";
return 0;
}
洛谷P3371 【模板】单源最短路径(弱化版)
最新推荐文章于 2024-07-24 14:05:37 发布