https://www.luogu.com.cn/problem/P4779
1 #define IO std::ios::sync_with_stdio(0) 2 #include <bits/stdc++.h> 3 using namespace std; 4 #define mk make_pair 5 #define pb push_back 6 const int inf=2147483647; 7 const int N=1e5+10; 8 9 struct node{ 10 int u,w; 11 bool operator <(const node&p)const{ 12 return w>p.w; 13 } 14 }; 15 16 vector<int>g[N],d[N]; 17 int dis[N]; 18 int n,m,s; 19 20 void dij(){ 21 priority_queue<node>q; 22 fill(dis+1,dis+n+1,inf); 23 dis[s]=0; 24 q.push(node{s,0}); 25 while(!q.empty()){ 26 node now=q.top(); 27 q.pop(); 28 int u=now.u; 29 if(now.w!=dis[u])continue;//优化,如果已经更新了与u相连的点通过u点到起点的距离那么无需再更新 30 for(int i=0;i<g[u].size();i++){ 31 int v=g[u][i]; 32 if(dis[u]+d[u][i]<dis[v]){ 33 dis[v]=dis[u]+d[u][i]; 34 q.push(node{v,dis[v]}); 35 } 36 } 37 } 38 } 39 int main(){ 40 IO; 41 cin>>n>>m>>s; 42 for(int i=1;i<=m;i++){ 43 int u,v,w; 44 cin>>u>>v>>w; 45 g[u].pb(v); 46 d[u].pb(w); 47 } 48 dij(); 49 for(int i=1;i<=n;i++)cout<<dis[i]<<" "; 50 }