Codeforces Round #131 (Div. 1) A - Game
1 second
256 megabytes
standard input
standard output
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.
The first line contains integer n (1 ≤ n ≤ 200) — the number of game parts. The next line contains n integers, the i-th integer — ci (1 ≤ ci ≤ 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 ki (0 ≤ ki ≤ n - 1), then ki distinct integers ai, j (1 ≤ ai, j ≤ n; ai, 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.
On a single line print the answer to the problem.
1
1
0
1
5
2 2 1 1 3
1 5
2 5 1
2 5 4
1 5
0
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.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
using namespace std;
#define INF 0x73737373
#define EPS 1e-8
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
int pos[], n, ret;
vector<int> pre[];
bool vis[];
bool pre_check(int index)
{
for(int i = ; i < pre[index].size(); i++)
if(!vis[pre[index][i]])return false;
return true;
}
bool all_complete()
{
for(int i = ; i <= n; i++)if(!vis[i])return false;
return true;
}
void work(int now, int cost)
{
while(true)
{
bool find = false;
for(int i = ; i <= n; i++)
{
if(vis[i])continue;
if(pre_check(i) && pos[i] == now)
{
vis[i] = true;
find = true;
cost++;
}
}
if(!find)break;
}
if(all_complete())
{
ret = min(ret, cost);
return;
}
work((now % ) + , cost + );
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)scanf("%d", &pos[i]);
for(int i = ; i <= n; i++)
{
int num;
scanf("%d", &num);
for(int j = ; j <= num; j++)
{
int a;
scanf("%d", &a);
pre[i].push_back(a);
}
}
ret = INF;
for(int i = ; i <= ; i++)
{
memset(vis, false, sizeof(vis));
vis[] = true;
work(i, );
}
printf("%d\n", ret);
return ;
}
Codeforces Round #131 (Div. 1) A - Game的更多相关文章
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- Codeforces Round #131 (Div. 2) E. Relay Race dp
题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...
- Codeforces Round #131 (Div. 2)
A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...
- Codeforces Round #131 (Div. 2) : B
首先能被2,5整除的数结尾必须是0: 如果没有0肯定不行: 然后判断他们的和ans%3: 如果==0,直接从大到小输出就行: 如果==1,要么删除它们之间最小的那个%3==1的,要么删除两个小的并且% ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- 64_t7
texlive-ulqda-bin-svn13663.0-33.20160520.fc26.2..> 24-May-2017 15:57 33102 texlive-ulqda-doc-svn2 ...
- js数据绑定(模板引擎原理)
<div> <ul id="list"> <li>11111111111</li> <li>22222222222< ...
- IOS使用SourceTree
一.安装sourceTree 1.下载 访问SourceTree 软件官方下载地址 : https://www.sourcetreeapp.com 下载macos版本 2.安装 安装和windows安 ...
- [USACO16OPEN]262144
传送门啦 其实大家可以先看一下这个题 [USACO16OPEN]248 分析: 数据范围很奇特:n特别,a[i]特别——如果O(N^3)能接受就直接区间DP水过了,但是不行,于是考虑设计一个状态囊括a ...
- 编译环境搭建:Makefile
前言 长久以来,笔者一直想用一种管理工具,将所编写的测试程序.算法代码以及工程代码统一管理起来.因为有些是用Java写的有些是用C++写的.虽有想法,但却无行动.这又让我想起了昨天晚上看到一部电影里所 ...
- Java标记接口
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------这篇博客主要来谈谈" ...
- SQLSERVER中的非工作时间不得插入数据的触发器的实现
create trigger trigger_nameon table_namefor insert,update,deleteasif (datepart(yy,getdate())%4=0 or ...
- Spark(八)JVM调优以及GC垃圾收集器
一JVM结构 1 Java内存结构 JVM内存结构主要有三大块:堆内存.方法区和栈. 堆内存是JVM中最大的一块由年轻代和老年代组成,而年轻代内存又被分成三部分,Eden空间.From Survivo ...
- python中发送邮件各种问题
其实问题主要集中的QQ企业邮箱中,特别坑爹...代码如下: from email.mime.multipart import MIMEMultipart from email.mime.base im ...
- ionic路由(页面切换)
ui-router 的工作原理非常类似于 Angular 的路由控制器,但它只关注状态. • 在应用程序的整个用户界面和导航中,一个状态对应于一个页面位置 • 通过定义controller.templ ...