Lexicopolis

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

요약
방향 그래프와 매우 큰 k가 주어질 때 s에서 t로 가는 길이 k 경로 중 간선 가중치 기준 사전순 최소 경로를 찾고, 없으면 -1을 출력하며, 있으면 x진법 해시를 1e9+7로 나눈 값을 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 그리디, 행렬, 이분 탐색
정답자
아직 제출이 없습니다

문제

Welcome to Lexicopolis, the ancient city of legends and treasures. The city is famous for its intricate network of one-way roads. There are nn intersections and mm one-way roads connecting the intersections. People can only travel from intersection u_iu\_i to intersection v_iv\_i along road ii, and road ii is associated with a magical number w_iw\_i. A path of length kk from intersection ss to tt is a sequence of roads e_1,e_2,…,e_ke\_1, e\_2, \dots ,e\_k that allows travel from intersection ss to intersection tt. A path is lexicographically smaller than another path if at the frst road where they have different magic numbers (not index), the number on the frst path is smaller than the number on the second path.

It is rumored that the tourist who figures out the lexicographically smallest path of length kk from intersection s to intersection tt can receive a gift from the Lexicopolis government. Please write a program to fnd the lexicographicall smallest path of length kk from intersection ss to tt. If it is impossible to travel from intersection ss to tt with exactly kk roads, output -1.

입력

The first line contains six integers nn, mm, ss, tt, xx, kk. nn is the number of intersections. mm is the number of roads. ss is the starting intersection and tt is the ending intersection. xx is a number that will be used for outputting the answer. kk is the length of path. The ii-th of the mm following lines contains three integers u_iu\_i, v_iv\_i and w_iw\_i. That means road ii is from intersection u_iu\_i to intersection v_iv\_i and associated with magic number w_iw\_i.

출력

If there is no path of length kk from intersection ss to tt, output -1. Otherwise, assume such a path exists. Consider the lexicographically smallest path e_1,e_2,…,e_ke\_1, e\_2, \dots ,e\_k, and output ∑k_i=1w_e_ixk−i\sum^k\_{i=1}{w\_{e\_i}x^{k-i}} modulo 109+710^9+7, where xx is the number provided as the fifth value in the first line of the input.

제한

  • 2≤n≤502 ≤ n ≤ 50
  • 1≤m≤n2−n1 ≤ m ≤ n^2 - n
  • 1≤u_i≤n1 ≤ u\_i ≤ n for i∈1,2,…,mi \in \\{1, 2, \dots ,m\\}
  • 1≤v_i≤n1 ≤ v\_i ≤ n for i∈1,2,…,mi \in \\{1, 2, \dots ,m\\}
  • 1≤w_i≤1091 ≤ w\_i ≤ 10^9 for i∈1,2,…,mi \in \\{1, 2, \dots ,m\\}
  • u_i≠v_iu\_i \ne v\_i for i∈1,2,…,mi \in \\{1, 2, \dots ,m\\}
  • (u_i,v_i)≠(u_j,v_j)(u\_i, v\_i) \ne (u\_j , v\_j ) for i≠ji \ne j
  • 1≤s≤n1 ≤ s ≤ n
  • 1≤t≤n1 ≤ t ≤ n
  • 1≤k≤1091 ≤ k ≤ 10^9
  • 1≤x≤1091 ≤ x ≤ 10^9

예제4

  1. 예제 1

    입력
    3 6 1 3 10 4
    1 2 2
    2 1 1
    1 3 1
    3 1 2
    2 3 1
    3 2 2
    
    예상 출력
    1211
    
  2. 예제 2

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

    입력
    6 7 5 6 10 10
    1 2 1
    2 4 2
    3 4 1
    4 5 3
    5 3 5
    4 6 2
    6 5 1
    
    예상 출력
    121513477
    
  4. 예제 4

    입력
    6 7 1 6 123 2
    1 2 1000000000
    2 4 2
    3 4 3
    4 5 4
    5 3 1
    4 6 2
    6 5 1
    
    예상 출력
    -1