#P3485. [POI 2009] BAJ-The Walk of Bytie-boy
[POI 2009] BAJ-The Walk of Bytie-boy
Description
You are given a directed graph with vertices and edges. Each edge has a letter on it. You are also given an integer and a sequence .
For each (), you need to find a shortest path from to 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 .
Input Format
The first line contains two integers .
Then follow lines. Each line contains two integers and a letter , indicating there is a directed edge from to , and the letter on this edge is .
The next line contains an integer .
The last line contains integers, giving the sequence .
Output Format
Output lines. On the -th line, output a path from to 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 of the testdata, , , , , .
It is also guaranteed that there are no multiple edges or self-loops.
Translated by ChatGPT 5
京公网安备 11011102002149号