#P4408. Finding Chris

    ID: 17654 Type: Default 1000ms 256MiB

Finding Chris

Finding Chris

Chris's parents are in a hurry to find their child Chris who is known to be hiding at either Shermie's or Yashiro's house. The city is modeled as a connected tree with N nodes and N-1 bidirectional roads. Each road x takes T_x minutes to traverse. Chris lives at node C, while his friends live at nodes A (Shermie) and B (Yashiro).

The parents always follow these two rules:

  1. If \(d(C,A) < d(C,B)\) then they first search Shermie’s house at node A and then Yashiro's house at node B; otherwise, they search Yashiro's house first.
  2. They always travel along the unique path between any two nodes.

The worst-case time spent will be when Chris is at the second friend’s house. Hence, if the first visited friend is chosen as \(F_1\) and the other is \(F_2\), the worst-case travel time is given by:

\[ T = d(C, F_1) + d(F_1, F_2) \]

Your task is to compute and output this worst-case time.

inputFormat

The first line contains four integers: N (number of nodes), A, B, and C representing the positions of Shermie's house, Yashiro's house, and Chris's house respectively. The nodes are 1-indexed.

Each of the next N-1 lines contains three integers U V T, indicating that there is a bidirectional road between nodes U and V which takes T minutes to traverse.

outputFormat

Output a single integer, the worst-case time (in minutes) required for Chris's parents to find him following the described search order.

sample

3 1 2 3
3 1 5
3 2 6
16