Machine Shop
시간 제한1초메모리 제한1024 MB
기계의 구매 가격과 조립에 필요한 부품 목록이 주어질 때, 기계 K를 얻는 최소 비용을 구한다. 조립 비용은 부품 비용의 합이다.
문제
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 and (), the number of different machines and the number of the machine Elise needs to obtain. The machines are numbered .
The following lines describe the machines, in the order of their numbers. First on each line is (), the cost of buying the machine . If the machine can only be obtained by buying it, the price is followed by a and there will be no more data on that line. Otherwise, the price is followed by (), the number of other machines needed to build the machine , and then by space-separated integers: the numbers of the other machines; these 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 .