This page is still under construction.

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

Flowery Trails

Interview

Time limit2sMemory limit256 MB

Summary
Add twice the length of every trail that lies on some shortest route from point 0 to point P-1.
Level

Medium4 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

A national park has PP points of interest and TT two-way trails that link them. The entrance is point 00 and the highest peak is point P−1P-1.

Visitors walk from the entrance to the highest peak along a shortest path. Different visitors choose different shortest paths, so every shortest path is used by someone. A trail is popular when it lies on at least one shortest path from the entrance to the highest peak.

The park manager plants flowers along both sides of every popular trail, so a popular trail of length ll metres needs 2l2l metres of flowers. Compute the total length of flowers needed.

Two points can be linked by more than one trail, and a trail can start and end at the same point. Each trail counts separately, and a popular trail counts once no matter how many shortest paths use it. At least one path from the entrance to the highest peak exists.

Input

The first line has two integers PP and TT.

Each of the next TT lines has three integers p1p_1, p2p_2, and ll, meaning that a two-way trail of length ll metres links point p1p_1 and point p2p_2. The two points are not necessarily distinct.

Integers on the same line are separated by a single space.

Output

Print one integer, the total length of flowers in metres.

Limits

  • 2≤P≤100002 \le P \le 10000, the number of points.
  • 1≤T≤2500001 \le T \le 250000, the number of trails.
  • 1≤l≤10001 \le l \le 1000, the length of a trail.
  • 0≤p1,p2≤P−10 \le p_1, p_2 \le P-1.

Examples2

  1. Example 1

    Input
    10 15
    0 1 580
    1 4 90
    1 4 90
    4 9 250
    4 2 510
    2 7 600
    7 3 200
    3 3 380
    3 0 150
    0 3 100
    7 8 500
    7 9 620
    9 6 510
    6 5 145
    5 9 160
    
    Expected output
    3860
    
  2. Example 2

    Input
    4 7
    0 1 1
    0 2 2
    0 3 10
    0 3 3
    1 3 2
    2 3 1
    1 1 1
    
    Expected output
    18