object Main {

  def main(args: Array[String]): Unit = {
    import scala.collection.mutable.ListBuffer
    import scala.collection.mutable.ArrayBuffer
    val sc = new java.util.Scanner(System.in)

    val INF = Int.MaxValue / 2 - 1
    val Vs, Es, r = sc.nextInt()
    val E = Array.fill(Vs)(ArrayBuffer[(Int, Int)]())
    (0 until Es) foreach { _ =>
      val u, v, w = sc.nextInt()
      E(u).append((v, w))
    }
    val W = Array.fill(Vs)(INF)

    def topologicalSort(E: Array[ArrayBuffer[(Int, Int)]]): Seq[Int] = {
      val sortedV = new ListBuffer[Int]()
      val visited = new Array[Boolean](Vs)
      def recursive(u: Int): Unit = {
        E(u) foreach { case (v, _) =>
          if (!visited(v)) {
            visited(v) = true
            recursive(v)
          }
        }
        sortedV.prepend(u)
      }
      (0 until Vs) foreach recursive
      sortedV
    }

    topologicalSort(E) foreach { u =>
      E(u) foreach { case (v, w) =>
        if (W(v) > W(u) + w)
          W(v) = W(u) + w
      }
    }
  }

}
