#include <bits/stdc++.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 Maybe {
   T x;
   bool valid;
   Maybe() : valid(false) {}
   Maybe(T x_) : x(x_), valid(true) {}
   operator T() const {
      assert( valid );
      return x;
   }
};

template<typename W_>
struct Solver5 {
   using W = W_;
   struct Edge {
      Edge(Int src_, Int dest_, W cost_)
         : src(src_), dest(dest_), cost(cost_) {}
      Int src, dest;
      W cost;
   };
   Int n;
   std::vector<std::vector<Edge>> fss;
   Solver5(Int n_)
      : n(n_+1), fss(n) {}
   void addEdge(Int src, Int dest, W cost) {
      fss[src].emplace_back(src, dest, cost);
   }
   const std::vector<Edge>& forwardEdges(Int v) const {
      return fss[v];
   }
};

struct Solver4 {
   template<typename T> std::vector<Maybe<typename T::W>> solve(const T& graph, Int begin) const {
      using W = typename T::W;
      using WI = std::tuple<W, Int>;
      Int n = graph.n;
      std::vector<Maybe<W>> xs(n);
      std::priority_queue<WI, std::vector<WI>, std::greater<WI>> q;
      q.emplace(0, begin);
      xs[begin] = 0;
      while( not q.empty() ) {
         W d; Int i; std::tie(d, i) = q.top(); q.pop();
         for(auto kc : graph.forwardEdges(i)) {
            Int k = kc.dest; W c = kc.cost;
            W nd = d + c;
            Maybe<W> t = xs[k];
            if( not t.valid or nd < t ) {
               xs[k] = nd;
               q.emplace(nd, k);
            }
         }
      }
      return xs;
   }
};

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      Int v, e, r; scan(v, e, r);
      Solver5<Int> graph(v);
      for(Int i = 0; i < e; ++i) {
         Int a, b, c; scan(a, b, c);
         graph.addEdge(a, b, c);
      }
      Solver4 dijkstra;
      auto xs = dijkstra.solve(graph, r);
      for(Int i = 0; i < v; ++i) {
         Maybe<Int> w = xs[i];
         if( not w.valid ) {
            puts("INF");
         }
         else {
            std::cout << w << std::endl;
         }
      }
   }
};

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

Measured memory_bytes: 25064