现有一份单源最短路模板题的代码如下:
#include<bits/stdc++.h>
using namespace std;
int n,m,s,cnt,head[100001],dis[100001];
bool vis[100001];
struct edge
{
int nxt,to,w;
}e[200001];
struct node
{
int pos,dis;
bool operator<(const node &x)const
{
return dis>x.dis;
}
};
void add(int u,int v,int w)
{
e[++cnt]=(edge){head[u],v,w};
head[u]=cnt;
}
void dijkstra(int s)
{
memset(dis,0x3f,sizeof(dis));
dis[s]=0;
priority_queue<node>Q;
Q.push((node){s,0});
while(Q.size())
{
int x=Q.top().pos;
Q.pop();
if(vis[x])continue;
vis[x]=true;
for(int i=head[x];i;i=e[i].nxt)
{
int y=e[i].to;
if(dis[y]>dis[x]+e[i].w)
{
dis[y]=dis[x]+e[i].w;
if(!vis[y])Q.push((node){y,dis[y]});
}
}
}
}
int read()
{
int x=0;
char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x;
}
int main()
{
n=read(),m=read(),s=read();
while(m--)
{
int u=read(),v=read(),w=read();
add(u,v,w);
}
dijkstra(s);
for(int i=1;i<=n;i++)printf("%d ",dis[i]);
return 0;
}
对这个有 n 个顶点、m 条边的带权有向简单图用 Dijkstra 算法计算单源最短路时,如果使用链式前向星和堆,那么时间复杂度是多少?
RT,不知道是 O(nlogn) 还是 O(mlogm) 或其他的,麻烦各位大佬不吝赐教。