Labelled Paths

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

요약
각 정점 t마다 s에서 t로 가는 경로 중 간선 레이블을 이어 붙인 문자열이 사전순으로 가장 작은 경로를 출력하고, 도달할 수 없으면 0을 출력한다.
난이도

어려움10점 중 8점

유형
문자열, 정렬, 그래프, 위상 정렬
정답자
아직 제출이 없습니다

문제

We are given a directed acyclic graph with nn vertices and mm edges. Each edge has a label (a string of lowercase letters; possibly even an empty string). We can now extend the concept of labels from edges to paths by defining the label of a path as the concatenation of the labels of the edges that constitute this path (in the same order in which they appear in the path). The smallest path from a start vertex ss to a destination vertex tt is the path (from ss to tt) whose label is lexicographically smallest (i.e. the earliest in lexicographical order) amongst all the paths from ss to tt. Write a program that, for a given ss, outputs the smallest paths from ss to tt for all vertices tt of the graph.

입력

The first line contains four space-separated integers: nn (the number of vertices), mm (the number of edges), dd (the length of the string AA, on which see below) and ss (the number of the start vertex). The vertices are numbered by integers from 1 to nn.

The second line contains a string AA, which is exactly dd characters long; all these characters are lowercase letters of the English alphabet. All the edge labels in our graph are substrings of the string AA.

The remaining mm lines describe the edges of the graph. The ii-th of these lines describes the ii-th edge and contains four space-separated integers: u_iu\_i (the start vertex of this edge), v_iv\_i (the end vertex of this edge), p_ip\_i and ℓ_i\ell\_i. The last two of these integers indicate that the label of this edge is the substring of AA that begins with the p_ip\_i-th character of AA and is ℓ_i\ell\_i characters long. For this purpose we consider the characters of AA to be indexed by integers from 1 to dd.

출력

Output nn lines, where the tt-th line (for t=1,…,nt = 1, \ldots, n) describes the smallest path from ss to tt. If there is no path from ss to tt, the line should contain only the integer 0 and nothing else. Otherwise the line should start with the number of vertices on the path (including vertices ss and tt), followed by the list of those vertices, separated by spaces. If there are several possible solutions, you may output any of them.

제한

  • 1≤s≤n≤6001 \le s \le n \le 600
  • 1≤m≤2,0001 \le m \le 2\\,000
  • 1≤d≤1061 \le d \le 10^6
  • 1≤u_i≤n1 \le u\_i \le n, 1≤v_i≤n1 \le v\_i \le n, u_i≠v_iu\_i \not= v\_i (for all i=1,…,mi = 1, \ldots, m)
  • 1≤p_i1 \le p\_i, 0≤ℓ_i0 \le \ell\_i, p_i+ℓ_i−1≤dp\_i + \ell\_i - 1 \le d (for all i=1,…,mi = 1, \ldots, m)
  • The graph is acyclic and has no parallel edges (i.e. from i≠ji \not= j it follows that u_i≠u_ju\_i \not= u\_j and/or v_i≠v_jv\_i \not= v\_j).

힌트

In this example, the edge 3→13 \to 1 has the label ab; the edge 1→41 \to 4 has the label a; the smallest path from 33 to 44 is 3→1→43 \to 1 \to 4, whose label is aba.

예제1

  1. 예제 1

    입력
    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
    
    예상 출력
    2 3 1
    2 3 2
    1 3
    3 3 1 4
    3 3 2 5