堆优化dijkstra,时间复杂度,我个人写习惯的模版。
#include<bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
#define fi first
#define se second
const int N=2e5+10;int read(){int x=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();return x*f;
}int n,m,s,head[N],tot,dist[N],vis[N];
priority_queue<PII,vector<PII>,greater<PII> > q;struct node{int to,nxt,w;
}edge[N];
void add(int x,int y,int w){edge[++tot].to=y;edge[tot].w=w;edge[tot].nxt=head[x];head[x]=tot;
}void dijkstra(int s){memset(dist,0x3f,sizeof(dist)),memset(vis,0,sizeof(vis));q.push(make_pair(0,s)),dist[s]=0;while(!q.empty()){int t=q.top().se;q.pop();if(vis[t]) continue;vis[t]=1;for(int i=head[t];i;i=edge[i].nxt){int y=edge[i].to;if(dist[y]>dist[t]+edge[i].w){dist[y]=dist[t]+edge[i].w;q.push(make_pair(dist[y],y));}}}
}int main(){n=read(),m=read(),s=read();for(int i=1;i<=m;i++){int x=read(),y=read(),w=read();add(x,y,w);}dijkstra(s);for(int i=1;i<=n;i++) cout<<dist[i]<<" ";return 0;
}