Under construction

Still being built. Things may change

Natural Navigation

Time limit6sMemory limit1024 MB

Summary
Find the least walking time from intersection 1 to n when the app picks colours and the user takes the worst footpath for each colour.
Level

Medium7 of 10

Topics
Shortest path, Graph, Hash map
Solved
No attempts yet

Problem

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?

Input

The first line contains nn (1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5), mm (1≤m≤5⋅1051 \leq m \leq 5 \cdot 10^5), and kk (1≤k≤1 0001 \leq k \leq 1\,000).

Then mm footpaths follow, each described by two lines:

  • One line with three integers uu, vv, and tt (1≤u,v≤n1 \leq u,v \leq n, 1≤t≤1061 \leq t \leq 10^6). The footpath leads from intersection uu to intersection vv and takes tt seconds to walk.
  • One line with an integer ℓ\ell (1≤ℓ≤k1 \leq \ell \leq k), followed by ℓ\ell distinct integers c1,…,cℓc_1, \dots, c_\ell (1≤ci≤k1 \leq c_i \leq k). These are the colours visible along the footpath.

The sum of ℓ\ell over all footpaths does not exceed 5⋅1055 \cdot 10^5. A footpath can lead back to its starting intersection, and several footpaths can connect the same pair of intersections. Intersection nn is not guaranteed to be reachable from intersection 11.

Output

If the user cannot be led to intersection nn, print impossible. Otherwise, print a single integer: the time in seconds to reach intersection nn. Only the time spent walking along the footpaths is counted.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    14
    
  2. Example 2

    Input
    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
    
    Expected output
    impossible