Under construction

Still being built. Things may change

Bronze Cow Party

Interview

Time limit1sMemory limit128 MB

Summary
Given a connected undirected weighted graph, find twice the largest shortest-path distance from a fixed farm X, which is the longest round trip any cow makes.
Level

Medium4 of 10

Topics
Shortest path, Graph, Heap, Greedy
Solved
No attempts yet

Problem

There are NN farms (1≤N≤10001 \le N \le 1000), numbered 11 through NN. From each farm, exactly one cow travels to a big cow party held at farm XX (1≤X≤N1 \le X \le N). The farms are connected by MM bidirectional roads (1≤M≤100,0001 \le M \le 100{,}000); it is always possible to travel between any two farms along the roads. Traversing road ii takes TiT_i (1≤Ti≤1001 \le T_i \le 100) units of time. Two farms may be directly connected by more than one road.

After every cow has gathered at farm XX, they realize that each of them left her party favors back home. They pause the party and send every cow home to fetch her favors and then return to farm XX. Each cow walks the fastest possible route to her home farm and back. The party stays paused until the last cow returns, so its suspension time equals the largest round-trip time among all the cows. What is this minimum suspension time?

Input

The first line contains three space-separated integers NN, MM, and XX.

Each of the next MM lines describes one road with three space-separated integers AiA_i, BiB_i, and TiT_i: road ii connects farms AiA_i and BiB_i and takes TiT_i units of time to traverse.

Output

Print one integer: the minimum amount of time the party must be suspended.

Hint

Because the roads are bidirectional, a cow's round trip to farm XX and back takes exactly twice the shortest distance between her farm and farm XX. So compute the shortest distance from farm XX to every farm with a single shortest-path search, then double the largest of those distances.

Examples3

  1. Example 1

    Input
    4 8 2
    1 2 7
    1 3 8
    1 4 4
    2 1 3
    2 3 1
    3 1 2
    3 4 6
    4 2 2
    
    Expected output
    6
    
  2. Example 2

    Input
    2 1 1
    1 2 5
    
    Expected output
    10
    
  3. Example 3

    Input
    5 4 1
    1 2 2
    2 3 2
    3 4 2
    4 5 2
    
    Expected output
    16