#P3821. Isaac

Isaac

Description

Determine whether Ju Runguo can pass the final level without taking damage under the above conditions. If he can, output the minimum required health BB for Isaac under Ju Runguo’s control; otherwise, output 'IMP0SSBLE!!'.

Input Format

The first line contains five numbers n,m,s,t,kn, m, s, t, k, representing that there are nn rooms in total, mm pairs of adjacent rooms together with the required health to traverse them, the start is ss, the finish is tt, and Isaac must arrive exactly at time kk.

Each of the next mm lines contains three numbers u,v,wu, v, w, meaning that room uu and room vv are adjacent, and the required health to traverse between the two rooms in both directions is ww.

The next line contains a single number aa, the number of monsters.

Then follow aa data blocks, each describing one monster’s roaming pattern.

In each block, the first number TT is the number of rooms in the monster’s roaming cycle, followed by TT room IDs that indicate that starting from time 00, the monster will move from the first room in order and periodically repeat the sequence.

Output Format

Output a single line: the minimum required health BB for Isaac under Ju Runguo’s control, or 'IMP0SSBLE!!'.

2 1 1 2 1
1 2 1
0

1

2 1 1 2 1
1 2 2
0

2
2 1 1 2 10000001
1 2 2
0

2
2 1 1 2 10000001
1 2 2
1
2
2 1

2

Hint

There are 2020 groups of testdata.

For 15%15\% of the testdata, a=0a = 0, k20k \leq 20.

For 25%25\% of the testdata, a3a \leq 3, k1500k \leq 1500.

For 50%50\% of the testdata, a3a \leq 3, k104k \leq 10^4.

For 70%70\% of the testdata, a20a \leq 20, k106k \leq 10^6.

For 85%85\% of the testdata, a30a \leq 30, k108k \leq 10^8.

For 100%100\% of the testdata, a30a \leq 30, k2109k \leq 2*10^9, 2T42 \leq T \leq 4, n50n \leq 50, m1250m \leq 1250.

All inputs are within the range of int.

All numbers are positive. There may be multiple edges; if an edge is given multiple times, the weight of its last occurrence prevails.

Translated by ChatGPT 5