// // dijkstra妯℃澘.cpp // algorithm // // Created by david.xu on 2018/8/6. // Copyright 漏 2018骞?david.xu. All rights reserved. // //不能有负边权! #include <stdio.h> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <vector> #define maxn 100010 #define pa pair<int,int> using namespace std; int n,m,s,dis[maxn]; bool vis[maxn]; priority_queue<pa,vector<pa>,greater<pa> > q; struct edge{ int val,to; }; vector<edge> e[maxn]; void dijkstra()//dis[i] 表示从起点到i的最短距离 { for(int i=1;i<=n;i++) dis[i]=1000000001; dis[s]=0; q.push(make_pair(0, s)); while(!q.empty()) { int x=q.top().second; q.pop(); if(vis[x]) continue; vis[x]=1; for(int i=0;i<e[x].size();i++) { int y=e[x][i].to; if(dis[x]+e[x][i].val<dis[y]) { dis[y]=dis[x]+e[x][i].val; q.push(make_pair(dis[y], y)); } } } } int main() { scanf("%d%d%d",&n,&m,&s); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); edge tmp; tmp.to=y; tmp.val=z; e[x].push_back(tmp); } dijkstra(); for(int i=1;i<=n;i++) printf("%d ",dis[i]); return 0; }