This page is still under construction.

Parts of this page are still being built. What you see may change.

Apple Delivery

Time limit1sMemory limit128 MB

Summary
Given a weighted undirected graph, find the shortest round trip from a start node that visits two given nodes in either order.
Level

Medium6 of 10

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

Problem

Bessie has two crisp red apples to deliver to two of her friends in the herd.

The pastures are numbered 11 through PP (1≤P≤100,0001 \le P \le 100{,}000) and are connected by CC bidirectional cowpaths (1≤C≤200,0001 \le C \le 200{,}000). Each cowpath connects two distinct pastures P1iP1_i and P2iP2_i and has length DiD_i. No cowpath leads from a pasture to itself. The sum of all cowpath lengths ∑Di\sum D_i does not exceed 2,000,000,0002{,}000{,}000{,}000. It is always possible to travel from any pasture to any other pasture.

Bessie starts at pasture PBPB and must deliver both apples by visiting the two distinct pastures PA1PA1 and PA2PA2 in either order. The three pastures PBPB, PA1PA1, and PA2PA2 are all distinct. She may reuse pastures and cowpaths she has already visited, and the distance traveled is the sum of the lengths of all cowpaths she uses.

Find the minimum total distance Bessie must travel to deliver both apples.

The map below illustrates pasture numbers (in brackets) together with the cowpaths and their lengths.

                3        2       2
           [1]-----[2]------[3]-----[4]
             \     / \              /
             7\   /4  \3           /2
               \ /     \          /
               [5]-----[6]------[7]
                    1       2

Input

The first line contains five space-separated integers CC, PP, PBPB, PA1PA1, and PA2PA2.

Each of the next CC lines describes cowpath ii with three integers P1iP1_i, P2iP2_i, and DiD_i: the two pastures it connects and its length.

Output

Print, on a single line, the minimum total distance Bessie must travel to deliver both apples.

Examples3

  1. Example 1

    Input
    9 7 5 1 4
    5 1 7
    6 7 2
    4 7 2
    5 6 1
    5 2 4
    4 3 2
    1 2 3
    3 2 2
    2 6 3
    
    Expected output
    12
    
  2. Example 2

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

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