The dijkstra heap optimization algorithm uses the small root heap to take out the node with the smallest dis[] value each time, and then expands the connected nodes
The heap optimization algorithm is suitable for sparse graphs (adjacency list storage) and the time complexity is O(mlogn)
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
typedef pair<int,int> PII;
int e[200005],w[200005],h[200005],ne[200005];
int vis[100005],dis[100005];
int idx=0;
int n,m,s;
void add(int a,int b,int c)
{
e[++idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx;
}
void dijkstra(int s)
{
for(int i=1;i<=n;i++)
{
dis[i]=inf;
vis[i]=0;
}
dis[s]=0;
priority_queue<PII,vector<PII>,greater<PII>> heap;
heap.push({0,s});
while(heap.size())
{
int t=heap.top().second;
heap.pop();
if(vis[t])
continue;
vis[t]=1;
for(int i=h[t];i;i=ne[i])
{
int j=e[i];
if(dis[j]>dis[t]+w[i])
dis[j]=dis[t]+w[i];
heap.push({dis[j],j});
}
}
}
int main()
{
cin>>n>>m>>s;
int u,v,w;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
}
dijkstra(s);
for(int i=1;i<=n;i++)
{
if(dis[i]==inf)
cout<<2147483647<<" ";
else
cout<<dis[i]<<" ";
}
return 0;
}