代码:
cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+10;
int n,m,A;
int dis[N];
int vis[N];
vector<pair<int,int> > e[N];
void spfa()
{
for(int i=1;i<=n;i++) dis[i] = 1e18;
queue<int > q;
q.push(A);
dis[A] = 0;
vis[A] = 1;
while(q.size())
{
int now = q.front();
q.pop();vis[now] = 0;
for(auto t:e[now])
{
int spot = t.first,w = t.second;
if(dis[spot]>dis[now]+w)
{
dis[spot] = dis[now]+w;
if(vis[spot]==0)
{
vis[spot] = 1;
q.push(spot);
}
}
}
}
}
signed main()
{
cin>>n>>m>>A;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
e[a].push_back({b,c});
e[b].push_back({a,c});
}
spfa();
for(int i=1;i<=n;i++)
{
if(dis[i]==1e18) cout<<(1ll<<31)-1;
else cout<<dis[i]<<" ";
}
}