Machine Shop | 프로그래밍의 벗 PivotOJ
PivotOJ

Machine Shop

시간 제한: 1000ms메모리 제한: 1024MB출처: EIO 2020-21 finalBOJ 29902

문제

Elise Isabella Oya works in the Cute Machine Shop. The shop sells many different machines. All of them can be bought from suppliers. Some of them can also be built in the shop from others; this may also be the case for the others in turn. For example, it may be possible to build the machine A from machines B and C, and the machine B in turn from machines D and E; thus, in this case, the machine A could also be built from machines C, D, and E.

Elise was asked for a price quote for a machine. To respond, she needs to know the lowest possible cost of obtaining the machine, whether by buying or by building it. The cost of labor in building a machine from others can be neglected.

입력

The first line contains space-separated integers NN and KK (1KN10001 \le K \le N \le 1\,000), the number of different machines and the number of the machine Elise needs to obtain. The machines are numbered 1,,N1, \ldots, N.

The following NN lines describe the machines, in the order of their numbers. First on each line is PiP_i (0Pi100000 \le P_i \le 10\,000), the cost of buying the machine ii. If the machine can only be obtained by buying it, the price is followed by a 00 and there will be no more data on that line. Otherwise, the price is followed by MiM_i (1MiN1 \le M_i \le N), the number of other machines needed to build the machine ii, and then by MiM_i space-separated integers: the numbers of the other machines; these MiM_i numbers will always be distinct. Additionally, it is known that no machine is a component of itself when it is built, neither directly nor indirectly (in graph-theoretical terms, we have a directed acyclic graph).

출력

Output exactly one integer: the minimal cost of obtaining the machine KK.

예제

예제 1

입력
1 1
10 0
출력
10

예제 2

입력
5 1
50 3 2 3 4
30 2 4 5
20 2 4 5
5 1 5
10 0
출력
35
코드를 제출하려면 로그인하세요.