juchenglin @ 2023-05-10 21:07:36
by juchenglin @ 2023-05-10 21:07:53
#include<bits/stdc++.h>
using namespace std;
const int N=100020;
const int inf=0x3f3f3f3f;
struct edge
{
int v,w,fq;
};
vector<edge> e[N+1];
int m,n,a,b,c,s;
int dis[N];
int flag;
void Bellman_Ford(int x)
{
memset(dis,inf,sizeof(dis));
dis[x] = 0;
for(int i=1;i<=n-1;i++)
{
flag = 0;
for(int u=1;u<=n;u++)
{
for(int j=0;j<e[u].size();j++)
{
int v=e[u][j].v,w=e[u][j].w;
if(dis[u]+w<dis[v])
{
dis[v]=dis[u]+w;
flag=1;
}
}
}
if(flag==0)
{
return;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>s;
for(int i=1;i<=m;i++)
{
cin>>a>>b>>c;
e[a].push_back((edge){b,c,a});
}
Bellman_Ford(s);
for(int i=1;i<=n;i++)
{
if(dis[i] == inf)
{
cout<<2147483647<<" ";
}
else
{
cout<<dis[i]<<" ";
}
}
return 0;
}