Under construction

Still being built. Things may change

Labelled Paths

Time limit15sMemory limit1024 MB

Summary
Given a DAG whose edge labels are substrings of one string A, find the lexicographically smallest path label from s to every vertex.
Level

Hard8 of 10

Topics
Dynamic programming, Topological sort, String matching, Graph
Solved
No attempts yet

Problem

We are given a directed acyclic graph with nn vertices and mm edges. Each edge has a label, a string of lowercase letters that may be empty. The label of a path is the concatenation of the edge labels along the path, in path order. The smallest path from a start vertex ss to a destination vertex tt is the path whose label is lexicographically smallest among all paths from ss to tt. For a given ss, output the smallest path from ss to every vertex tt.

Input

The first line contains four integers nn, mm, dd, and ss. Here dd is the length of the string AA described below. Vertices are numbered from 1 to nn.

The second line contains the string AA, exactly dd lowercase English letters. Every edge label is a substring of AA.

Each of the next mm lines contains four integers uiu_i, viv_i, pip_i, and ℓi\ell_i. The ii-th edge goes from uiu_i to viv_i, and its label is the substring of AA that starts at the pip_i-th character and has length ℓi\ell_i. The characters of AA are indexed from 1 to dd.

Output

Print nn lines. The tt-th line describes the smallest path from ss to tt. If no path from ss to tt exists, print only the integer 0. Otherwise, print the number of vertices on the path (including ss and tt), followed by the vertices in path order, separated by spaces. If several solutions exist, any of them is accepted.

Constraints

  • 1≤s≤n≤6001 \le s \le n \le 600
  • 1≤m≤20001 \le m \le 2000
  • 1≤d≤1061 \le d \le 10^6
  • 1≤ui≤n1 \le u_i \le n, 1≤vi≤n1 \le v_i \le n, ui≠viu_i \ne v_i for all ii
  • 1≤pi1 \le p_i, 0≤ℓi0 \le \ell_i, pi+ℓi−1≤dp_i + \ell_i - 1 \le d for all ii
  • The graph is acyclic and has no parallel edges. That is, for i≠ji \ne j, ui≠uju_i \ne u_j or vi≠vjv_i \ne v_j.

Hint

In this example, the edge 3→13 \to 1 has label ab and the edge 1→41 \to 4 has label a. The smallest path from 3 to 4 is 3→1→43 \to 1 \to 4, with label aba.

Examples1

  1. Example 1

    Input
    5 7 6 3
    abcbca
    3 2 1 1
    2 1 5 1
    2 5 4 2
    3 1 1 2
    3 4 3 2
    1 4 6 1
    5 4 5 2
    
    Expected output
    2 3 1
    2 3 2
    1 3
    3 3 1 4
    3 3 2 5