1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
const int NR=100000;
int d[NR]/*最短路径存储列表*/,n,m,s;
vector< pair<int,int> > adj[NR];//一个结构体队列
void spfa(int s){//spfa函数
memset(d,0x3f,sizeof(d));
queue<int> q;
q.push(s);d[s]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<adj[u].size();i++){
int v=adj[u][i].first;
int w=adj[u][i].second;
if(d[u]+w<d[v]){
d[v]=d[u]+w;
q.push(v);
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=0;i<m;i++){
int st,en,w;
scanf("%d%d%d",&st,&en,&w);
adj[st].push_back(make_pair(en,w));
}
spfa(s);
for(int i=1;i<=n;i++){//输出
if(d[i]>=1e9)printf("2147483647 ");
else printf("%d ",d[i]);
}
return 0;
}
|