Travel


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

The subway system in Barareh consists of nn stations and mm subway lines. The stations are numbered from 11 to nn. Each line is managed by a company and each company has a unique identifier. The ithi-th subway line (1im)(1 \leq i \leq m) is managed by company cic_i and connects only two stations, pip_i and qiq_i, in both directions. When arriving at a station, if more than one subway line is connected to that station, passengers can easily switch between lines. The cost of using the subway in Barareh is a bit different.

When a passenger uses only subway lines operated by a single company, he only need to pay 11 Bararehian dollar. However, if a passenger changes their subway line at a station, and these two lines are managed by different companies, he have to pay an additional 11 Bararehian dollar. In other words, the passenger pays 11 Bararehian dollar at the first station, and then at each intermediate station, they only need to pay an additional 11 Bararehian dollar if the two lines used for arriving and departing the station belong to different companies.

Calculate the minimum cost required to travel from station 11 to station nn.

ورودی🔗

The first line of the input consists of two integers nn (2n105)(2 \leq n \leq 10^5) and mm (0m2105)(0 \leq m \leq 2*10^5) separated by a space, which is the number of stations and the number of subway lines, respectively. Then, for each 1im1 \leq i \leq m , the (i+1)th(i + 1)-th line of input contains three integers pip_i, qiq_i, and cic_i (1ci106)(1 \leq ci \leq 10^6), separated by a space (piqi,1pi,qin)(p_i \neq q_i, 1 \leq p_i,q_i \leq n).

خروجی🔗

In the only line of output, if it is impossible to travel from station 11 to station nn using the subway lines, print 11. Otherwise, print the minimum cost required to travel from station 11 to station nn.

ورودی نمونه 1🔗

3 3 
1 2 1 
2 3 1 
3 1 2
Plain text

خروجی نمونه 1🔗

1
Plain text

ورودی نمونه 2🔗

8 11 
1 3 1 
1 4 2 
2 3 1 
2 5 1 
3 4 3 
3 6 3 
3 7 3 
4 8 4 
5 6 1 
6 7 5 
7 8 5
Plain text

خروجی نمونه 2🔗

2
Plain text

In the example above, initially, one can traverse the following path by paying 11 Bararehian dollar: 1>3>2>5>61->3->2->5->6 Then, with another 11 Bararehian dollar payment, continue the path as follows: 6>7>86-> 7 ->8.

ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.