#P16051. [ICPC 2022 NAC] Transparency

[ICPC 2022 NAC] Transparency

题目描述

You must audit a factory that produces a number of products. Each product is subject to its own taxation rules and regulations. Unfortunately, deciding which rules apply to a product is not obvious since it can be difficult to distinguish the products from one another. It is your job to investigate if the factory is being completely transparent in its production process by determining if the factory is capable of producing two different products that are indistinguishable from one another.

The final products are represented by sequences of uppercase letters (AZ) and lowercase letters (az). Two final products are indistinguishable from one another if the products are equal after removing all lowercase letters. For example, WabXYcz is indistinguishable from gWXfbY, since after removing all lowercase letters we are left with WXY in both instances.

A factory is modelled as a set of stations SS, a set of directed and labelled transition edges TT, an initial station s0s_0, and a set of packaging stations PP. The initial station is in the set of stations, i.e., s0Ss_0 \in S, and the set of packaging stations is a subset of SS, i.e., PSP \subseteq S. A transition edge is defined by a 3-tuple (s1,σ,s2)(s_1, \sigma, s_2), where s1,s2Ss_1, s_2 \in S are stations, and σ\sigma is an uppercase or lowercase letter. The existence of the transition (s1,σ,s2)(s_1, \sigma, s_2) in TT means that if in producing some product we are at station s1s_1, then appending σ\sigma to the product will leave us at station s2s_2. That is, there is a directed edge from station s1s_1 to station s2s_2, labelled σ\sigma.

A product can be produced by the factory if by starting at station s0s_0 there is a sequence of edges that can be followed, ending at a station in PP, whose edge labels can be concatenated, in order, to create the product. The sequence of edges can be empty if s0Ps_0 \in P. For example, the following strings can all be produced by the factory described in the first sample input and illustrated in the figure: AF, FAFB, AbFFAd, AdydAd. Note that this is not an exhaustive list.

Each production transition can be used an unlimited number of times to make a product. It is guaranteed that for each station, ss, and letter, σ\sigma, there is at most one directed edge from station ss labelled σ\sigma. That is, (s1,σ,s2),(s1,σ,s3)T(s_1, \sigma, s_2), (s_1, \sigma, s_3) \in T implies s2=s3s_2 = s_3. It is possible that transitions will go back to the same station; that is, (s1,σ,s1)T(s_1, \sigma, s_1) \in T is allowed.

Given a factory design, determine if the factory is capable of producing two distinct but indistinguishable products. If there is such a pair of products, then report the sum of the lengths of the strings in the pair that minimizes the sum of the lengths. If there is no such pair, print 1-1.

:::align{center}

The factory design from the first sample input. The packaging station is marked by a double circle. :::

输入格式

The first line of input contains three integers, ss (1s501 \le s \le 50), pp (1ps1 \le p \le s) and tt (1t52s1 \le t \le 52 \cdot s), where ss is the number of stations, pp is the number of packaging stations, and tt is the number of transitions. The set of stations is numbered from 11 to ss.

Each of the next pp lines contains a single integer ii (1is1 \le i \le s). These are the packing stations. All values of ii are distinct.

Each of the next tt lines is of the form:

s1σs2s_1\quad \sigma\quad s_2

where s1s_1 and s2s_2 (1s1,s2s1 \le s_1, s_2 \le s) are stations, and σ\sigma is a single character, consisting of an uppercase or lowercase letter. These are the transitions. The initial station is always station 11. There will never be two transitions out of the same state labelled with the same character.

输出格式

Output a single line with a single integer. If there is no pair of distinct, indistinguishable products, then output 1-1. If there exists a pair of distinct, indistinguishable products, then output the smallest sum a+b|a| + |b| where aa and bb are strings corresponding to distinct, indistinguishable products.

4 1 8
3
1 F 1
1 A 2
2 b 1
2 F 3
2 d 3
3 B 3
3 y 4
4 d 1
10
4 1 8
3
1 F 1
1 A 2
2 b 1
2 F 3
2 d 3
3 B 3
3 y 4
4 d 4
-1
5 2 5
1
5
1 i 2
2 c 3
3 p 4
4 c 1
1 I 5
4