아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Machine Shop

시간 제한1초메모리 제한1024 MB

요약
기계의 구매 가격과 조립에 필요한 부품 목록이 주어질 때, 기계 K를 얻는 최소 비용을 구한다. 조립 비용은 부품 비용의 합이다.
난이도

보통10점 중 5점

유형
그래프, 동적 계획법, DFS, 위상 정렬
정답자
아직 제출이 없습니다

문제

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 (1≤K≤N≤1,0001 \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 P_iP\_i (0≤P_i≤10,0000 \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 M_iM\_i (1≤M_i≤N1 \le M\_i \le N), the number of other machines needed to build the machine ii, and then by M_iM\_i space-separated integers: the numbers of the other machines; these M_iM\_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.

예제2

  1. 예제 1

    입력
    1 1
    10 0
    
    예상 출력
    10
    
  2. 예제 2

    입력
    5 1
    50 3 2 3 4
    30 2 4 5
    20 2 4 5
    5 1 5
    10 0
    
    예상 출력
    35