#include <iostream>
#include <queue>
using namespace std;
const int inf = 2147483647;
const int MAXX = 2e5 + 11;
int n,m,s,cnt;
int dis[MAXX];
int to[MAXX],nxt[MAXX],val[MAXX],h[MAXX];
bool vis[MAXX];
struct node{
int v,w;
friend bool operator < (node a,node b){
return a.w > b.w;
}
} tmp;
priority_queue <node> q;
void add(int u,int v,int w){
cnt ++;
to[cnt] = v;
val[cnt] = w;
nxt[cnt] = h[u];
h[u] = cnt;
}
void dijkstra(){
for (int i = 1;i <= n;i ++){
dis[i] = inf;
}
dis[s] = 0;
tmp.v = s,tmp.w = 0;
q.push(tmp);
while(!q.empty()){
int u = q.top().v;
q.pop();
if (vis[u]){
continue;
}
vis[u] = 1;
for (int i = h[u];i;i = nxt[i]){
if (dis[to[i]] > (long long)dis[u] + val[i]){
dis[to[i]] = dis[u] + val[i];
tmp.w = dis[to[i]];
tmp.v = to[i];
q.push(tmp);
}
}
}
}
signed main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> m >> s;
for (int i = 1,u,v,w;i <= m;i ++){
cin >> u >> v >> w;
add(u,v,w);
}
dijkstra();
for (int i = 1;i <= n;i ++){
cout << dis[i] << " ";
}
return 0;
}
标签:tmp,MAXX,val,int,单源,Dijkstra,cnt,模板,dis
From: https://www.cnblogs.com/Xssion37-XY/p/18238837