import sys

import queue





class Dijkstra:

    class Edge:

        def __init__(self, end, cost):

            self.to = end

            self.cost = cost



    def __init__(self, node_size, inf):

        self._node = node_size

        self._graph = [[] for _ in range(self._node)]

        self.inf = inf

        self.dist = [self.inf for _ in range(self._node)]



    def add_edge(self, st, ed, cs):

        self._graph[st].append(self.Edge(ed, cs))



    def solve(self, start):

        que = queue.PriorityQueue()

        self.dist[start] = 0

        que.put((0, start))

        while not que.empty():

            cur_cost, cur_vertex = que.get()

            if self.dist[cur_vertex] < cur_cost:

                continue

            for e in self._graph[cur_vertex]:

                if self.dist[e.to] > cur_cost + e.cost:

                    self.dist[e.to] = cur_cost + e.cost

                    que.put((self.dist[e.to], e.to))





if __name__ == '__main__':

    V, E, r = list(map(int, sys.stdin.readline().split()))

    dk = Dijkstra(V, 10 ** 10)

    for i in range(E):

        s, t, d = list(map(int, sys.stdin.readline().split()))

        dk.add_edge(s, t, d)

    dk.solve(r)

    for value in dk.dist:

        if value == dk.inf:

            print("INF")

        else:

            print(value)

import sys

from heapq import heappush, heappop





class Dijkstra:

    class Edge:

        def __init__(self, end, cost):

            self.to = end

            self.cost = cost



    def __init__(self, node_size, inf):

        self._node = node_size

        self._graph = [[] for _ in range(self._node)]

        self.inf = inf

        self.dist = [self.inf for _ in range(self._node)]



    def add_edge(self, st, ed, cs):

        self._graph[st].append(self.Edge(ed, cs))



    def solve(self, start):

        que = []

        self.dist[start] = 0

        heappush(que, (0, start))

        while que:

            cur_cost, cur_vertex = heappop(que)

            if self.dist[cur_vertex] < cur_cost:

                continue

            for e in self._graph[cur_vertex]:

                if self.dist[e.to] > cur_cost + e.cost:

                    self.dist[e.to] = cur_cost + e.cost

                    heappush(que, (self.dist[e.to], e.to))





if __name__ == '__main__':

    V, E, r = list(map(int, sys.stdin.readline().split()))

    dk = Dijkstra(V, 10 ** 10)

    for i in range(E):

        s, t, d = list(map(int, sys.stdin.readline().split()))

        dk.add_edge(s, t, d)

    dk.solve(r)

    for value in dk.dist:

        if value == dk.inf:

            sys.stdout.write("INF\n")

        else:

            sys.stdout.write("%s\n" % value)

import sys
import queue


class Dijkstra:
    class Edge:
        def __init__(self, end, cost):
            self.to = end
            self.cost = cost

    def __init__(self, node_size, inf):
        self._node = node_size
        self._graph = [[] for _ in range(self._node)]
        self.inf = inf
        self.dist = [self.inf for _ in range(self._node)]

    def add_edge(self, st, ed, cs):
        self._graph[st].append(self.Edge(ed, cs))

    def solve(self, start):
        que = queue.PriorityQueue()
        self.dist[start] = 0
        que.put((0, start))
        while not que.empty():
            cur_cost, cur_vertex = que.get()
            if self.dist[cur_vertex] < cur_cost:
                continue
            for e in self._graph[cur_vertex]:
                if self.dist[e.to] > cur_cost + e.cost:
                    self.dist[e.to] = cur_cost + e.cost
                    que.put((self.dist[e.to], e.to))


if __name__ == "__main__":
    V, E, r = list(map(int, sys.stdin.readline().split()))
    dk = Dijkstra(V, 10**10)
    for i in range(E):
        s, t, d = list(map(int, sys.stdin.readline().split()))
        dk.add_edge(s, t, d)
    dk.solve(r)
    for value in dk.dist:
        if value == dk.inf:
            print("INF")
        else:
            print(value)

import sys
from heapq import heappush, heappop


class Dijkstra:
    class Edge:
        def __init__(self, end, cost):
            self.to = end
            self.cost = cost

    def __init__(self, node_size, inf):
        self._node = node_size
        self._graph = [[] for _ in range(self._node)]
        self.inf = inf
        self.dist = [self.inf for _ in range(self._node)]

    def add_edge(self, st, ed, cs):
        self._graph[st].append(self.Edge(ed, cs))

    def solve(self, start):
        que = []
        self.dist[start] = 0
        heappush(que, (0, start))
        while que:
            cur_cost, cur_vertex = heappop(que)
            if self.dist[cur_vertex] < cur_cost:
                continue
            for e in self._graph[cur_vertex]:
                if self.dist[e.to] > cur_cost + e.cost:
                    self.dist[e.to] = cur_cost + e.cost
                    heappush(que, (self.dist[e.to], e.to))


if __name__ == "__main__":
    V, E, r = list(map(int, sys.stdin.readline().split()))
    dk = Dijkstra(V, 10**10)
    for i in range(E):
        s, t, d = list(map(int, sys.stdin.readline().split()))
        dk.add_edge(s, t, d)
    dk.solve(r)
    for value in dk.dist:
        if value == dk.inf:
            sys.stdout.write("INF\n")
        else:
            sys.stdout.write("%s\n" % value)

-import queue +from heapq import heappush, heappop -        que = queue.PriorityQueue() +        que = [] -        que.put((0, start)) -        while not que.empty(): -            cur_cost, cur_vertex = que.get() +        heappush(que, (0, start)) +        while que: +            cur_cost, cur_vertex = heappop(que) -                    que.put((self.dist[e.to], e.to)) +                    heappush(que, (self.dist[e.to], e.to)) -            print("INF") +            sys.stdout.write("INF\n") -            print(value) +            sys.stdout.write("%s\n" % value)