题目链接https://www.luogu.org/problem/P3371
模版题没什么好说的,标程:(来自洛谷博主KesdiaelKen)
#include<bits/stdc++.h>
using namespace std;
struct edge
{
int z;
int val;
int nexty;
}edge[1000000];
int head[20000];
int cnt=0;
inline void add(int a,int b,int c)
{
cnt++;
edge[cnt].z=b;
edge[cnt].val=c;
edge[cnt].nexty=head[a];
head[a]=cnt;
}
int main()
{
bool visit[20000]={0};
long long dis[20000];
int n,m,s;
int a,b,c;
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=n;i++) dis[i]=2147483647;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
int curr=s;
dis[s]=0;
long long minn;
while(!visit[curr])
{
visit[curr]=true;
for(int i=head[curr];i!=0;i=edge[i].nexty)
{
if(!visit[edge[i].z]&&dis[edge[i].z]>dis[curr]+edge[i].val)
dis[edge[i].z]=dis[curr]+edge[i].val;
}
minn=2147483647;
for(int i=1;i<=n;i++)
{
if(!visit[i]&&minn>dis[i])
{
minn=dis[i];
curr=i;
}
}
}
for(int i=1;i<=n;i++) printf("%lld ",dis[i]);
return 0;
}