#P3485. [POI 2009] BAJ-The Walk of Bytie-boy

    ID: 2540 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2009POISpecial Judge广度优先搜索,BFS

[POI 2009] BAJ-The Walk of Bytie-boy

Description

You are given a directed graph with nn vertices and mm edges. Each edge has a letter on it. You are also given an integer dd and a sequence s1,s2,,sds_1, s_2, \dots, s_d.

For each ii (1i<d1 \le i < d), you need to find a shortest path from sis_i to si+1s_{i+1} such that the letters on the edges of this path, concatenated in order, form a palindrome.

It is not guaranteed that each vertex appears at most once in ss.

Input Format

The first line contains two integers n,mn, m.

Then follow mm lines. Each line contains two integers xi,yix_i, y_i and a letter cic_i, indicating there is a directed edge from xix_i to yiy_i, and the letter on this edge is cic_i.

The next line contains an integer dd.

The last line contains dd integers, giving the sequence ss.

Output Format

Output d1d-1 lines. On the ii-th line, output a path from sis_i to si+1s_{i+1} that satisfies the condition.

If no such path exists, output -1; otherwise, output all letters on this path.

If there are multiple valid paths, output any one of them.

6 7
1 2 a
1 3 x
1 4 b
2 6 l
3 5 y
4 5 z
6 5 a
3
1 5 3

3 ala
-1

Hint

For 100%100\% of the testdata, 2n4002 \le n \le 400, 1m6×1041 \le m \le 6\times 10^4, 1xi,yin1 \le x_i, y_i \le n, 2d1002 \le d \le 100, 1sin1 \le s_i \le n.

It is also guaranteed that there are no multiple edges or self-loops.

Translated by ChatGPT 5