C++#include<bits/stdc++.h>
using namespace std;
constexpr int N=3e5+2,inf=0x3f3f3f3f;
int head[N],nxt[N<<1],to[N<<1],weight[N<<1],tot;
int dis[N],cnt[N],n,e;bitset<N> vis;
inline void add(int u,int v,int w=1){
to[++tot]=v;weight[tot]=w;
nxt[tot]=head[u];head[u]=tot;
return;
}
inline bool spfa(const int&s){
memset(dis,0x3f,sizeof dis);
memset(cnt,0,sizeof cnt);
vis.reset();dis[s]=0;
queue<int> q;
q.push(s);vis[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];i;i=nxt[i]){
int v=to[i],w=weight[i];
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;++cnt[v];
if(cnt[v]>=n) return true;
if(!vis[v]) q.push(v),vis[v]=1;
if(dis[q.front()]>dis[q.back()]) swap(q.front(),q.back());
}
}
}
return false;
}
int main(){
scanf("%d%d",&n,&e);
while(e--){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);//add(v,u,w);
}
int s;scanf("%d",&s);
long long c=clock();
if(spfa(s)) printf("Can't find the shortest path.\n");
else for(int i=1;i<=n;i++){
if(dis[i]==inf) printf("No path from %d to %d.\n",s,i);
else printf("From %d to %d cost %d.\n",s,i,dis[i]);
}
return 0;
}