Problem11357--HDU3986 - Harry Potter and the Final Battle

11357: HDU3986 - Harry Potter and the Final Battle

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

The final battle is coming. Now Harry Potter is located at city $1$, and Voldemort is located at city $n$.

To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city $1$ and city $n$.

But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one.

Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city $n$ and begin the battle in the worst case.

Input

First line, case number $t\ (t\leq 20)$.

Then for each case: an integer $n\ (2\leq n\leq 1000)$ means the number of city in the magical world, the cities are numbered from $1$ to $n$. 

Then an integer $m$ means the roads in the magical world, $m\ (0< m \leq 50,000)$.

Following $m$ lines, each line with three integer $u, v, w\ (u != v,\ 1 \leq u, v\leq n,\ 1\leq w <1000)$, separated by a single space. It means there is a bidirectional road between $u$ and $v$ with the cost of time $w$. There may be multiple roads between two cities.

Output

Each case per line: the shortest time to reach city $n$ in the worst case. If it is impossible to reach city n in the worst case, output “-1”.

Sample 1 Input

3
4
4
1 2 5
2 4 10
1 3 3
3 4 8
3
2
1 2 5
2 3 10
2
2
1 2 1
1 2 2

Sample 1 Output

15
-1
2

HINT

HDU3986.

Source/Category