图论
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 10000+100;
const int maxm = 500000+100;
const int inf = 0x3f3f3f3f;
int d[maxn];
int head[maxn],vis[maxn];
int to[maxm],edge[maxm],nxt[maxm];
int tot=0;
int n,m,s;
void addedge(int x,int y,int z) {
to[++tot]=y;
edge[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa(int s) {
for(int i=1;i<=n;i++) d[i] = inf;
d[s]=0;
for(int i=1;i<=n;i++) vis[i]=0;
vis[s]=1;
deque<int> Q;
Q.push_back(s);
while(!Q.empty()) {
int now = Q.front();Q.pop_front();
vis[now] = 0;
for(int i=head[now];i;i=nxt[i]) {
int y=to[i],z=edge[i];
if(d[y]>d[now]+z) {
d[y]=d[now]+z;
if(vis[y]==0) {
if(d[y]<d[Q.front()]) {
Q.push_front(y);
}else Q.push_back(y);
}
}
}
}
}
int main() {
cin>>n>>m>>s;
int x,y,z;
for(int i=1;i<=m;i++) {
cin>>x>>y>>z;
addedge(x,y,z);
}
spfa(s);
for(int i=1;i<=n;i++) {
if(d[i]==inf) printf("2147483647 ");
else printf("%d ",d[i]);
}
printf("\n");
return 0;
}
数据结构