#include <bits/stdc++.h>
#include <inttypes.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
#define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)
typedef int64_t Int;
typedef std::vector<Int> LI;
const double pi = 3.14159265358979323846264338;
const Int mod = 1000000007;

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(Int& x) { if( scanf("%" PRId64, &x) < 0 ) exit(0); }
template<> void scan1(std::string& x) { if( not ( std::cin >> x ) ) exit(0); }
template<> void scan1(LI& xs) { for(Int &x : xs) scan1(x); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

template<typename T>
struct Q002 {
   Int v;
   std::vector<std::vector<std::tuple<Int, T>>> graph;
   Q002(Int v_) : v(v_), graph(v) {}
   void set(Int a, Int b, T c) {
      graph[a].emplace_back(b, c);
   }
   const std::vector<std::tuple<Int, T>>& nexts(Int a) const {
      return graph[a];
   }
};

template<typename T>
struct F002 {
   std::tuple<std::vector<T>, std::vector<bool>> operator()(const Q002<T>& graph, Int begin) const {
      Int v = graph.v;
      std::vector<bool> checked(v, false);
      std::vector<T> xs(v);
      std::priority_queue<std::tuple<T, Int>, std::vector<std::tuple<T, Int>>, std::greater<std::tuple<T, Int>>> q;
      q.push(std::make_tuple(0, begin));
      xs[begin] = 0;
      checked[begin] = true;
      while( not q.empty() ) {
         Int i; T d;
         std::tie(d, i) = q.top(); q.pop();
         dumpll(i, d);
         for(auto kc : graph.nexts(i)) {
            Int k; T c;
            std::tie(k, c) = kc;
            T nd = d + c;
            if( not checked[k] or nd < xs[k] ) {
               xs[k] = nd;
               dumpl(nd);
               checked[k] = true;
               q.push(std::make_tuple(nd, k));
            }
         }
      }
      return std::make_tuple(xs, checked);
   }
};

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      Int v, e, r; scan(v, e, r);
      Q002<Int> graph(v);
      for(Int i = 0; i < e; ++i) {
         Int a, b, c; scan(a, b, c);
         graph.set(a, b, c);
      }
      F002<Int> dijkstra;
      std::vector<Int> dists;
      std::vector<bool> checked;
      std::tie(dists, checked) = dijkstra(graph, r);
      for(Int i = 0; i < v; ++i) {
         if( not checked[i] ) puts("INF");
         else printf("%ld\n", dists[i]);
      }
   }
};

int main() {
   loop std::unique_ptr<Solver>(new Solver())->solve();
}

Measured memory_bytes: 19468