Parcel Delivery

Time limit1sMemory limit128 MB

Summary
Given a weighted undirected graph, find the minimum total edge weight along a path from barn 1 to barn N.
Level

Easy3 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Farmer Hyunseo has to deliver a parcel to Farmer Chanhong, and he is just about to set off. To pass through peacefully, he must feed tasty fodder to every cow he meets along the way. But Hyunseo is a miser, so he wants to travel while meeting as few cows as possible.

Hyunseo has a map. It shows NN (1≤N≤50000)(1 \le N \le 50000) barns and MM (1≤M≤50000)(1 \le M \le 50000) two-way paths that connect them. The ii-th path has CiC_i (0≤Ci≤1000)(0 \le C_i \le 1000) cows on it and connects two distinct barns AiA_i and BiB_i (1≤Ai,Bi≤N, Ai≠Bi)(1 \le A_i, B_i \le N,\ A_i \ne B_i). Two barns may be connected by more than one path. Hyunseo is at barn 11, and Chanhong is at barn NN.

Refer to the map below.

           [2]---
          / |    \
         /1 |     \ 6
        /   |      \
     [1]   0|    --[3]
        \   |   /     \2
        4\  |  /4      [6]
          \ | /       /1
           [4]-----[5]
                3

On this map, the best route Hyunseo can take is 1→2→4→5→61 \to 2 \to 4 \to 5 \to 6, for which the total number of cows he meets is 1+0+3+1=51 + 0 + 3 + 1 = 5.

Given Hyunseo's map and the amount of fodder he must give whenever he meets cows on a path, find the minimum total fodder he must give while traveling from barn 11 to barn NN. The travel distance is not taken into account.

Input

The first line contains two integers NN and MM, separated by a space.

Each of the next MM lines contains three integers AiA_i, BiB_i, and CiC_i, meaning that the path connecting barn AiA_i and barn BiB_i has CiC_i cows on it.

Output

Print the minimum total fodder Hyunseo must give while traveling from barn 11 to barn NN.

Examples3

  1. Example 1

    Input
    6 8
    4 5 3
    2 4 0
    4 1 4
    2 1 1
    5 6 1
    3 6 2
    3 2 6
    3 4 4
    
    Expected output
    5
    
  2. Example 2

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

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