This page is still under construction.

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

Roadblocks

Time limit1sMemory limit128 MB

Summary
Find the length of the second-shortest walk from vertex 1 to vertex N in an undirected weighted graph, where walks may repeat edges.
Level

Medium6 of 10

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

Problem

Bessie has moved to a small farm and sometimes likes to walk back to visit one of her best friends. She does not want to arrive too quickly, because she enjoys the scenery along the way, so she has decided to travel the second-shortest path instead of the shortest one. Such a path is guaranteed to exist.

The countryside has NN intersections, numbered 11 through NN, connected by RR bidirectional roads. Each road joins two intersections and has a positive length. Bessie starts at intersection 11, and her friend lives at intersection NN.

A path here is any walk from 11 to NN: it may reuse roads or intersections, and it may even backtrack over a road it has already used. The length of a path is the sum of the lengths of the roads it uses. The second-shortest path is a path whose length is strictly greater than the length of the shortest path, yet no greater than the length of any other such path. In other words, if LL is the shortest achievable length, the answer is the smallest achievable length that is strictly larger than LL. (If several different routes share the shortest length LL, they all count as shortest; the second-shortest length is still the next larger achievable value.)

Constraints: 1≤N≤50001 \le N \le 5000 and 1≤R≤100,0001 \le R \le 100{,}000.

Input

  • Line 1: two space-separated integers NN and RR.
  • Lines 2 to R+1R+1: each line contains three space-separated integers AA, BB, and DD, describing a bidirectional road between intersections AA and BB with length DD (1≤D≤50001 \le D \le 5000).

Output

  • Print a single line containing the length of the second-shortest path from intersection 11 to intersection NN.

Hint

In the sample, the shortest path is 1→2→41 \to 2 \to 4 with length 100+200=300100 + 200 = 300, and the next-longer path is 1→2→3→41 \to 2 \to 3 \to 4 with length 100+250+100=450100 + 250 + 100 = 450, so the second-shortest length is 450450.

Examples1

  1. Example 1

    Input
    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100
    
    Expected output
    450