Ice_Fist @ 2024-08-15 14:48:56
#include<iostream>
using namespace std;
int head[100000],cnt;
long long dist[1000000];
bool vis[1000000];
int m,n,s;
struct edge{
int to;
int nextt;
int wei;
}edge[1000000];
void add(int x,int y,int z){
edge[++cnt].to=y;
edge[cnt].wei=z;
edge[cnt].nextt=head[x];
head[x]=cnt;
}
int main(){
cin>>m>>n>>s;
for(int i=1;i<=n;i++){
dist[i]=2147483647;
}
dist[s]=0;
for(int i=1;i<=n;i++){
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
}
int pos=s;
while(vis[pos]==0){
long long minn=2147483647;
vis[pos]=1;
for(int i=head[pos];i!=0;i=edge[i].nextt){
if(!vis[edge[i].to]&&dist[edge[i].to]>dist[pos]+edge[i].wei){
dist[edge[i].to]=dist[pos]+edge[i].wei;
}
}
for(int i=1;i<=m;i++){
if(dist[i]<minn&&vis[i]==0){
minn=dist[i];
pos=i;
}
}
}
for(int i=1;i<=m;i++){
cout<<dist[i]<<' ';
}
}
by linch @ 2024-08-15 14:54:51
@Ice_Fist 您的代码时间复杂度为
请使用堆优化使时间复杂度降为
by qazsedcrfvgyhnujijn @ 2024-08-15 14:56:04
写堆优化版的,时间复杂度 priority_queue
版
暴力 Dijkstra 是