There and Back Again

시간 제한2초메모리 제한1024 MB

요약
도시 1과 n 사이를 잇는 두 경로의 사용 도로 집합이 서로 다르도록 하면서 총 이동 시간을 최소로 만드는 값을 구하거나 -1을 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 완전 탐색
정답자
아직 제출이 없습니다

문제

There are nn cities in Asia-Pacific, numbered from 11 to nn. The 2024 ICPC Asia Pacific Championship is held in Hanoi, which is city nn.

There are mm bidirectional roads, numbered from 11 to mm, connecting some pairs of cities. Road ii connects cities u_iu\_i and v_iv\_i and takes t_it\_i units of time to travel in either direction. Each road connects different cities and different roads connect different pairs of cities.

You live in city 11. You would like to travel to city nn to attend the contest through a sequence of roads, and then travel back to city 11 through a sequence of roads. Traveling through the same route is boring, so you would like the routes in both traversals to be different. Two routes are considered different if the set of distinct roads traversed through one route is different from the set of distinct roads traversed through the other route.

In each traversal, it is possible to pass through the same city or road multiple times. It is also possible to continue traversing after reaching the destination city (i.e., city 11 or city nn). The traversal time is the sum of the travel times of the roads passed through in the traversal. If a road is passed through multiple times in the traversal, then the travel time of the road is also counted multiple times accordingly.

Determine the minimum total traversal time to do both traversals satisfying the requirements above, or indicate if the requirements cannot be satisfied.

입력

The first line of input contains two integers nn and mm (2≤n≤100,0002 ≤ n ≤ 100\\, 000; 1≤m≤min⁡(n(n−1)2,300,000)1 ≤ m ≤ \min(\frac{n(n-1)}{2}, 300\\, 000)). Each of the next m lines contains three integers. The ii-th line contains u_iu\_i, v_iv\_i, and t_it\_i (1≤u_i<v_i≤n1 ≤ u\_i < v\_i ≤ n; 1≤t_i≤10001 ≤ t\_i ≤ 1000). Different roads connect different pairs of cities.

출력

Output an integer representing the minimum total traversal time to do both traversals satisfying the requirements above, or -1 if the requirements cannot be satisfied.

예제4

  1. 예제 1

    입력
    3 2
    1 2 10
    1 3 5
    
    예상 출력
    30
    
  2. 예제 2

    입력
    4 3
    1 2 10
    2 3 5
    3 4 2
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    4 4
    1 2 3
    2 4 2
    1 3 3
    3 4 4
    
    예상 출력
    12
    
  4. 예제 4

    입력
    3 1
    1 2 1000
    
    예상 출력
    -1