F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Age of Moyu

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5161    Accepted Submission(s): 1525


Problem Description
Mr.Quin love fishes so much and Mr.Quin¡¯s city has a nautical system,consisiting of $N$ ports and $M$ shipping lines. The ports are numbered $1$ to $N$. Each line is occupied by a Weitian. Each Weitian has an identification number.

The $i$-th $(1 \leq i \le M)$ line connects port $A_i$ and $B_i$ $(A_i \ne B_i )$ bidirectionally, and occupied by $C_i$ Weitian (At most one line between two ports).

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is $1$ XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of $1$ XiangXiangJi. In a case where Mr.Quin changed from some Weitian $A$'s line to another Weitian's line changes to Weitian $A$'s line again, the additional cost is incurred again.

Mr.Quin is now at port $1$ and wants to travel to port $N$ where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can¡¯t travel to port $N$, print $-1$ instead)
 

Input
There might be multiple test cases, no more than $20$. You need to read till the end of input.

For each test case,In the first line, two integers $N$ $(2 \le N \le 100000)$ and $M$ $(0 \le M \le 200000)$, representing the number of ports and shipping lines in the city.

In the following m lines, each contain three integers, the first and second representing two ends $A_i$ and $B_i$ of a shipping line $(1 \le A_i, B_i \le N)$ and the third representing the identification number $C_i$ $(1 \le C_i \le 1000000)$ of Weitian who occupies this shipping line.
 

Output
For each test case output the minimum required cost. If Mr.Quin can¡¯t travel to port $N$, output $-1$ instead.
 

Sample Input
3 3 1 2 1 1 3 2 2 3 1 2 0 3 2 1 2 1 2 3 2
 

Sample Output
1 -1 2
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-20 12:03:11, Gzip enabled