The Admiral

Time limit1sMemory limit128 MB

Summary
Find two vertex- and edge-disjoint (except at endpoints) directed paths from node 1 to node v in a weighted graph minimizing total edge weight, which requires a min-cost flow formulation with vertex splitting.
Level

Hard8 of 10

Topics
Shortest path, Graph, Greedy
Solved
No attempts yet

Statement

Michiel de Ruyter is the most famous admiral in the history of the Netherlands. He distinguished himself in the Anglo-Dutch Wars of the 17th century.

Graph theory began to be studied during de Ruyter's lifetime, and the admiral often applied it to his naval battle plans. Each intermediate point at sea is represented as a vertex, and every sea route leading from one point to another is a directed edge. Between two points uu and ww there is at most one route u→wu \to w. The weight of each edge is the number of cannonballs that must be fired to pass along that route safely.

De Ruyter's most famous tactic is the "De Ruyter Manoeuvre". In it, two warships leave a single point heading in different directions. Each ship moves while fighting enemy vessels, and the two reunite at the destination. The two ships must always choose non-overlapping routes: apart from the start and the destination, they may not pass through the same intermediate point or use the same route.

De Ruyter dislikes wasting money, so he wants to choose the two ships' routes so that the total number of cannonballs fired is as small as possible.

Input

The input consists of several test cases. The end of the input is marked by end-of-file (EOF).

The first line of each test case contains the number of intermediate points vv and the number of routes ee (3≤v≤10003 \le v \le 1000, 3≤e≤100003 \le e \le 10000). Each of the next ee lines contains the description of a route aia_i, bib_i, cic_i (1≤ai,bi≤v1 \le a_i, b_i \le v, ai≠bia_i \ne b_i, 1≤ci≤1001 \le c_i \le 100). Here aia_i is the starting point of the route, bib_i is its ending point, and cic_i is the number of cannonballs that must be fired to travel along it.

The tactic starts at point 11 and ends at point vv. There are always at least two non-overlapping paths between points 11 and vv.

Output

For each test case, print on its own line the minimum total number of cannonballs the two warships must fire while following the tactic.

Note

In the first test case the two ships (red and blue) start at point 11 and meet at point 66. The red ship travels 1→3→61 \to 3 \to 6 (3333 cannonballs) and the blue ship travels 1→2→5→4→61 \to 2 \to 5 \to 4 \to 6 (5353 cannonballs). Apart from the start and the end, the two paths share no vertex or edge, and the total is 8686 cannonballs.

Examples4

  1. Example 1

    Input
    6 11
    1 2 23
    1 3 12
    1 4 99
    2 5 17
    2 6 73
    3 5 3
    3 6 21
    4 6 8
    5 2 33
    5 4 5
    6 5 20
    3 3
    1 3 1
    1 2 5
    2 3 5
    
    Expected output
    86
    11
    
  2. Example 2

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

    Input
    4 4
    1 2 5
    2 4 5
    1 3 7
    3 4 7
    
    Expected output
    24
    
  4. Example 4

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