PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000471. Các hành tinh (Planets)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các hành tinh (Planets)

Planets

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Jack starts on planet 1 at time zero and must reach planet nn. Bidirectional gates have positive integer travel durations. Each planet has blocked integer departure times. If departure is blocked, wait one second and repeat until departure is allowed. Minimize the arrival time; no departure is required at the destination.

Input

Read n,mn,m, then mm triples ai,bi,cia_i,b_i,c_i describing a gate. The next nn lines each contain kik_i followed by kik_i strictly increasing blocked times tijt_{ij}.

Output

Print the minimum arrival time, or -1 if unreachable.

Subtasks

Subtask 1 (20 points)

  • 2≤n≤802\le n\le80, 0≤m≤2000\le m\le200, ∑ki≤200\sum k_i\le200
  • 1≤ai,bi≤n1\le a_i,b_i\le n, ai≠bia_i\ne b_i, 1≤ci≤1041\le c_i\le10^4; no parallel edges; 0≤ki≤1050\le k_i\le10^5, 0≤tij<1090\le t_{ij}<10^9, each list is strictly increasing; all data are integers.

Subtask 2 (30 points)

  • 2≤n≤20002\le n\le2000, 0≤m≤50000\le m\le5000, ∑ki≤5000\sum k_i\le5000
  • 1≤ai,bi≤n1\le a_i,b_i\le n, ai≠bia_i\ne b_i, 1≤ci≤1041\le c_i\le10^4; no parallel edges; 0≤ki≤1050\le k_i\le10^5, 0≤tij<1090\le t_{ij}<10^9, each list is strictly increasing; all data are integers.

Subtask 3 (50 points)

  • 2≤n≤1052\le n\le10^5, 0≤m≤1050\le m\le10^5, ∑ki≤105\sum k_i\le10^5
  • 1≤ai,bi≤n1\le a_i,b_i\le n, ai≠bia_i\ne b_i, 1≤ci≤1041\le c_i\le10^4; no parallel edges; 0≤ki≤1050\le k_i\le10^5, 0≤tij<1090\le t_{ij}<10^9, each list is strictly increasing; all data are integers.

Examples

Example 1

Input

4 6
1 2 2
1 3 3
1 4 8
2 3 4
2 4 5
3 4 3
0
1 3
2 3 4
0

Output

7

Explanation

Via planet 2, depart at time 2 and travel five more seconds. Planet 3 requires waiting until time 5; the direct route costs eight.

Example 2

Input

3 1
1 2 3
0
1 3
0

Output

-1

Explanation

Planet 3 is unreachable.