CF213A.Game
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies.
Rubik has 3 computers, on which he can play this game. All computers are located in different houses. Besides, it has turned out that each part of the game can be completed only on one of these computers. Let's number the computers with integers from 1 to 3. Rubik can perform the following actions:
- Complete some part of the game on some computer. Rubik spends exactly 1 hour on completing any part on any computer.
- Move from the 1-st computer to the 2-nd one. Rubik spends exactly 1 hour on that.
- Move from the 1-st computer to the 3-rd one. Rubik spends exactly 2 hours on that.
- Move from the 2-nd computer to the 1-st one. Rubik spends exactly 2 hours on that.
- Move from the 2-nd computer to the 3-rd one. Rubik spends exactly 1 hour on that.
- Move from the 3-rd computer to the 1-st one. Rubik spends exactly 1 hour on that.
- Move from the 3-rd computer to the 2-nd one. Rubik spends exactly 2 hours on that.
Help Rubik to find the minimum number of hours he will need to complete all parts of the game. Initially Rubik can be located at the computer he considers necessary.
弗里克和鲁比克喜欢玩电脑游戏。弗里克最近发现了一款令鲁比克非常感兴趣的新游戏。该游戏由 n 个关卡组成,而完成每个关卡可能需要先完成其他若干关卡。已知该游戏可以被完全通关,即其关卡之间不存在循环依赖。
鲁比克有三台电脑,他可以在这些电脑上玩这款游戏。这三台电脑分别位于不同的住宅中。此外,人们还发现:游戏的每个关卡只能在其中某一台特定的电脑上完成。我们用整数 1 至 3 对这三台电脑编号。鲁比克可以执行以下操作:
- 在某台电脑上完成游戏的某个关卡。鲁比克在任意一台电脑上完成任意一个关卡均恰好耗时 1 小时。
- 从第 1 台电脑移动到第 2 台电脑。鲁比克完成该移动恰好耗时 1 小时。
- 从第 1 台电脑移动到第 3 台电脑。鲁比克完成该移动恰好耗时 2 小时。
- 从第 2 台电脑移动到第 1 台电脑。鲁比克完成该移动恰好耗时 2 小时。
- 从第 2 台电脑移动到第 3 台电脑。鲁比克完成该移动恰好耗时 1 小时。
- 从第 3 台电脑移动到第 1 台电脑。鲁比克完成该移动恰好耗时 1 小时。
- 从第 3 台电脑移动到第 2 台电脑。鲁比克完成该移动恰好耗时 2 小时。
请帮助鲁比克计算出他完成全部游戏关卡所需的最少小时数。初始时,鲁比克可位于他所认为合适的任意一台电脑上。
输入格式
The first line contains integer n (1 ≤ n ≤ 200) — the number of game parts. The next line contains n integers, the i-th integer — c__i (1 ≤ c__i ≤ 3) represents the number of the computer, on which you can complete the game part number i.
Next n lines contain descriptions of game parts. The i-th line first contains integer k__i (0 ≤ k__i ≤ n - 1), then k__i distinct integers a__i, j (1 ≤ a__i, j ≤ n; a__i, j ≠ i) — the numbers of parts to complete before part i.
Numbers on all lines are separated by single spaces. You can assume that the parts of the game are numbered from 1 to n in some way. It is guaranteed that there are no cyclic dependencies between the parts of the game.
第一行包含一个整数 n(1≤n≤200)—— 表示游戏关卡的数量。
第二行包含 n 个整数,其中第 i 个整数 ci(1≤ci≤3)表示可以完成第 i 个关卡的计算机编号。
接下来 n 行描述各游戏关卡。第 i 行首先包含一个整数 ki(0≤ki≤n−1),然后是 ki 个互不相同的整数 ai,j(1≤ai,j≤n;且 ai,j=i)—— 表示在完成第 i 个关卡前必须先完成的关卡编号。
每行中的数字均以单个空格分隔。你可以假设游戏关卡编号为 1 到 n。题目保证游戏关卡之间不存在循环依赖。
输出格式
On a single line print the answer to the problem.
在一行中输出问题的答案。
输入输出样例
输入#1
1 1 0
输出#1
1
输入#2
5 2 2 1 1 3 1 5 2 5 1 2 5 4 1 5 0
输出#2
7
说明/提示
Note to the second sample: before the beginning of the game the best strategy is to stand by the third computer. First we complete part 5. Then we go to the 1-st computer and complete parts 3 and 4. Then we go to the 2-nd computer and complete parts 1 and 2. In total we get 1+1+2+1+2, which equals 7 hours.
第二个样例的说明:游戏开始前,最优策略是待在第三台电脑旁。首先我们完成第 5 个任务;然后前往第 1 台电脑,完成第 3 和第 4 个任务;接着前往第 2 台电脑,完成第 1 和第 2 个任务。总共耗时为 1+1+2+1+2,即 7 小时。
输入解题思路,AI测评打分。不知道怎么写?