#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second

//int dxy[5] = {0, 1, 0, -1, 0};
// assign avl ncm dijkstra geo2 kruskal graph uf lca BIT
// matrix dinic

typedef int Weight;
typedef int Flow;
struct Edge {
    int s, d; Weight w; Flow c;
    Edge() {};
    Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w), c(w) {};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) { return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')'); }

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void addArc(Graph &g, int s, int d, int w = 1) {
    g[s].push_back(Edge(s, d, w));
}
void addEdge(Graph &g, int a, int b, int w = 1) {
    addArc(g, a, b, w);
    addArc(g, b, a, w);
}

//???????§????????????????(???????????????)
//Dijkstra O((|E|+|V|)log|V|)
//dist: ?§????????????????????????§??????????????¢
//?????????: ??????????????¨????????????(??????-1)
vector<int> dijkstra(const Graph &g, int s, Array &dist) {
    int n = g.size();
    assert(s < n);
    enum { WHITE, GRAY, BLACK };
    vector<int> color(n, WHITE); color[s] = GRAY;
    vector<int> prev(n, -1);
    dist.assign(n, INF); dist[s] = 0;
    //using State = tuple<Weight, int, int>; //?§????????????????????????¢ ??? ???
    //typedef tuple<Weight, int, int> State;
    typedef pair< int, pair < int, int > > State;
    priority_queue<State, vector<State>, greater<State> > pq;
    pq.push(State(0, i_i(s, -1)));
    while (pq.size()) {
        Weight d = pq.top().first; 
        int v = pq.top().second.first, u = pq.top().second.second; 
        pq.pop();
        //tie(d, v, u) = pq.top(); pq.pop();
        if (dist[v] < d)continue;
        color[v] = BLACK; prev[v] = u;
        for (int i = 0; i < g[v].size(); i++) {
            Edge e = g[v][i];
        //for (auto &e : g[v]) {
            if (color[e.d] == BLACK)continue;
            if (dist[e.d] > dist[v] + e.w) {
                dist[e.d] = dist[v] + e.w;
                pq.push(State(dist[e.d], i_i(e.d, v)));
                //pq.emplace(dist[e.d], e.d, v);
                color[e.d] = GRAY;
            }
        }
    }
    return prev;
}

signed main()
{
    int2(n, m);
    Int(st);
    Graph g(n);

    rep(i, m) {
        int2(s, t);
        Int(c);
        addArc(g, s, t, c);
    }

    vi dist;
    dijkstra(g, st, dist);
    rep(i, n) {
        if (dist[i] == INF) {
            std::cout << "INF" << std::endl;
        } else {
            std::cout << dist[i] << std::endl;
        }
    }

    return 0;
}

Measured memory_bytes: 32084