Under construction

Still being built. Things may change

Twilight

Time limit4sMemory limit1024 MB

Summary
Find the shortest route from city 1 to every city that never walks the consecutive roads of any of the K forbidden paths.
Level

Medium7 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Ostania is a country with NN cities and MM roads connecting them. The cities are numbered from 11 to NN. The ii-th road (1≤i≤M)(1\le i\le M) is a one-way road from city uiu_i to city viv_i, and traveling along it takes wiw_i time. Between any two distinct cities, there is at most one road in each direction. For convenience, let w(u,v)w(u,v) be the time needed to travel along the road from city uu to city vv.

Westalis is a country in a Cold War rivalry with Ostania. Through several agents, Westalis obtained KK pieces of information about Ostania. Each piece is represented by a simple path. Precisely, the jj-th piece (1≤j≤K)(1\le j\le K) is a sequence of sjs_j distinct cities (pj,1,pj,2,⋯ ,pj,sj)(p_{j,1},p_{j,2},\cdots ,p_{j,s_j}), and for every 1≤t<sj1\le t<s_j there is a road from city pj,tp_{j,t} to city pj,t+1p_{j,t+1}. No city belongs to two different pieces of information at the same time.

Twilight, the agent codenamed , is currently in city 11 of Ostania. The Westalis government orders Twilight to travel to city xx to collect information. To maximize the efficiency of the collection, Twilight decides to take a route that contains none of the KK simple paths. Specifically, let (q1,q2,⋯ ,ql)(q_1,q_2,\cdots ,q_l) be the cities Twilight visits in order. Then all of the following must hold.

  1. There is a road from city qiq_i to city qi+1q_{i+1}. (1≤i<l)(1\le i<l)
  2. There are no integers ii and zz such that (pz,1,pz,2,⋯ ,pz,sz)=(qi,qi+1,⋯ ,qi+sz−1)(p_{z,1},p_{z,2},\cdots ,p_{z,s_z}) =(q_i,q_{i+1},\cdots ,q_{i+s_z-1}).
  3. Among all routes that satisfy conditions 1 and 2, the total time to reach city xx, (=∑i=1l−1w(qi,qi+1))(=\sum_{i=1}^{l-1}w(q_i,q_{i+1})), is minimal.

For every integer xx with 1≤x≤N1\le x\le N, determine whether Twilight can reach city xx, and if so, compute the time it takes to reach city xx.

Input

The first line contains three integers NN, MM, and KK separated by spaces. (2≤N≤2×105;(2\le N\le 2\times 10^5; 1≤M≤3×105;1\le M\le 3\times 10^5; 0≤K≤N2)0\le K\le\frac{N}{2} )

Each of the next MM lines contains uiu_i, viv_i, and wiw_i separated by spaces. For each ii, the road from city uiu_i to city viv_i is unique. (1≤i≤M;(1\le i\le M; 1≤ui,vi≤N;1\le u_i,v_i\le N; 1≤wi≤109;1\le w_i\le 10^9; wiw_i is an integer;; ui≠vi)u_i\neq v_i)

Line 1+M+j1+M+j contains sjs_j followed by the sjs_j integers pj,1,⋯ ,pj,sjp_{j,1}, \cdots, p_{j,s_j} of the simple path, separated by spaces. For the KK simple paths, each city appears at most once, and it is guaranteed that the path can be traversed along the roads in the order given in the input. (1≤j≤K;(1\le j\le K; sj≥2;s_j\ge 2; ∑j=1Ksj≤N)\sum_{j=1}^{K}s_j\le N)

Output

Print NN integers separated by spaces on the first line. The ii-th integer is the time it takes Twilight to reach city ii while satisfying all the conditions. If city ii cannot be reached, print -1.

Examples3

  1. Example 1

    Input
    4 4 1
    1 2 2
    1 3 1
    2 4 2
    3 4 5
    2 2 4
    
    Expected output
    0 2 1 6
    
  2. Example 2

    Input
    4 4 2
    1 2 2
    1 3 1
    2 4 2
    3 4 5
    2 1 3
    2 2 4
    
    Expected output
    0 2 -1 -1
    
  3. Example 3

    Input
    11 12 3
    1 2 40
    2 3 40
    3 1 40
    2 4 20
    3 6 10
    9 1 1
    10 11 1
    3 7 1
    7 6 2
    6 7 3
    7 8 4
    4 5 3
    4 1 2 4 5
    3 3 7 8
    2 10 11
    
    Expected output
    0 40 80 60 -1 83 81 90 -1 -1 -1