This page is still under construction.

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

Water Pipe Construction

Interview

Time limit8sMemory limit512 MB

Summary
Given a directed weighted graph, find the minimum total cost of two paths from source s to goals g1 and g2, where the two paths may share edges whose cost is counted once.
Level

Medium7 of 10

Topics
Graph, Shortest path, Dynamic programming, Greedy
Solved
No attempts yet

Problem

In 21XX, humanity finally began its plan to migrate to Mars. Selected for the first wave of Martian settlers, you are assigned to the Administrative Center of Mars, where you handle the various problems that arise on Mars. The Administrative Center's biggest immediate problem is securing a self-sufficient supply and demand cycle. Aid shipments from the Moon take months to arrive, so demand on Mars must in principle be met on Mars itself. On top of that, resources must be conserved as much as possible until a circulation system is established.

The Administrative Center has begun mining ice at the polar regions. The final goal is to melt this ice with sunlight and supply it as water to the individual bases. As a first step, it was decided to lay water pipes from the base with the water source to two major bases. In addition, at present only some bases and the roads connecting them have been developed, and laying water pipes in undeveloped areas would require enormous cost and fuel, so the decision was made to lay the water pipes along the roads. Furthermore, due to technical constraints, water in their pipes always flows in only one direction.

Your job is to write a program that minimizes the cost of laying the water pipes under these conditions.

Input

The input consists of multiple data sets.

The first line of a data set consists of 5 integers. In order, they are the number of bases on Mars n (3 ≤ n ≤ 100), the number of roads connecting bases m (2 ≤ m ≤ 1000), the number of the base that is the water source s, and the numbers of the two major bases that are the destinations of the water pipes g1 and g2. Bases are numbered with integers from 1 to n. s, g1, and g2 are all distinct.

The following m lines give information about the roads on which water pipes can be laid. Each line represents the information of a road between two bases and consists of 3 integers b1, b2, c (1 ≤ c ≤ 1000). Here b1 and b2 are the numbers of the start and end bases of the road, and they are distinct. c is the cost of laying a water pipe from base b1 to base b2.

For every data set, you may assume that a path supplying water from the source to the destinations always exists. Also, since there is at most one road between any two bases, cost information is given at most once for each direction.

The end of the input is indicated by a line containing 5 zeros separated by space characters.

Output

For each data set, output the minimum cost of laying the water pipes on one line.

Examples1

  1. Example 1

    Input
    4 5 1 3 4
    1 2 5
    2 3 5
    2 4 5
    1 3 8
    1 4 8
    0 0 0 0 0
    
    Expected output
    15