PivotOJ

Natural Navigation

시간 제한: 6000ms메모리 제한: 1024MB출처: GCPC 2021BOJ 25258

문제

You want to build an app to help people navigate through a large botanic garden. This is difficult, because there are many winding footpaths and intersections offering many choices, making traditional directions such as "turn right" or "move further north" unsuitable. Instead, the app should rely on the garden's greatest resource: the numerous exotic plants and their diverse colours. Whenever a user is at an intersection, the app will know where they are and will display one particular colour accordingly. The user will then follow a footpath where this colour is visible. If the colour can be spotted along multiple footpaths originating from the intersection, the user is free to choose any of these footpaths.

You have been given a perfect model of the botanic garden, consisting of nn intersections (numbered from 11 to nn) and mm footpaths going between those. To keep order, each footpath can only be used in the given direction. Currently, the plants are exhibiting kk different colours (numbered from 11 to kk) and for each footpath, you are given a list of all the colours that are visible along it when viewed from the intersection where it starts. A user is currently at intersection 11 and wants to navigate to intersection nn. You can assume that the user will follow the app's directions perfectly, but whenever faced with multiple options (because the given colour is visible along multiple footpaths), you have to assume they will make the worst possible choice. How long will it take to reach the target when your app gives the best possible instructions?

입력

The input consists of:

  • A line containing the number of intersections nn (1n51051 \leq n \leq 5 \cdot 10^5), the number of footpaths mm (1m51051 \leq m \leq 5 \cdot 10^5) and the number of distinct colours kk (1k10001 \leq k \leq 1\,000).
  • mm pairs of lines describing the directed footpaths, each formatted as follows:
    • One line with three integers uu, vv and tt (1u,vn,1t1061 \le u,v \le n, 1 \le t \le 10^6), meaning that the footpath leads from intersection uu to intersection vv and it takes tt seconds to walk along this footpath.
    • One line with an integer \ell (1k1 \leq \ell \leq k), followed by \ell distinct integers c1,,cc_1,\dots,c_\ell (1cik1 \le c_i \le k for each ii), listing the colours that appear along this footpath.

The sum of \ell over all footpaths does not exceed 51055 \cdot 10^5. Note that, as you would imagine in a botanic garden, a footpath can lead back to the intersection it started from and multiple footpaths can exist between a pair of intersections. Moreover, it is not guaranteed that each intersection can be reached via the footpaths.

출력

If it is impossible to lead the user to intersection nn, output impossible. Otherwise output a single integer, the time it will take to reach the target in seconds. We are only considering the time spent walking along the footpaths.

예제

예제 1

입력
4 6 2
1 2 6
1 1
1 3 3
1 2
2 3 5
1 2
2 4 8
1 1
3 1 4
2 1 2
3 4 3
1 1
출력
14

예제 2

입력
3 4 3
1 2 300
2 1 2
2 1 2000
2 3 1
1 3 80
2 2 1
2 2 42
1 2
출력
impossible
코드를 제출하려면 로그인하세요.