【CodeForces - 214C 】Game (拓扑排序,思维)

题干:

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.

Input

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 ≤ nai, 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.

Output

On a single line print the answer to the problem.

Examples

Input

1
1
0

Output

1

Input

5
2 2 1 1 3
1 5
2 5 1
2 5 4
1 5
0

Output

7

Note

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.

 

题目大意:

现在有三个工作站,有三种工作,每种工作需要完成前置任务才能进行当前工作,三个工作站之间转换需要花费时间,问将所有任务都完成需要花费的最少时间。一开始可以在任意一个工作站开始工作。

解题报告:

   通过他给的那个转化时间我们就知道这里面不简单、、、应该有可以化简的地方。一看果然,因为啊你会发现按照1->2->3->1的方式循环,每次都需要1单位时间,但是如果反过来,任意一个步骤都需要花费2时间,所以我们就想啊,比如2->1花费2单位时间,那么我们为什么不2->3->1呢?也是2时间,并且还多操作了一个机器的。。。肯定比之前的方案要优秀啊。所以根据数学归纳法我们发现就按照1->2->3->1->2->3->1这样的方式循环就好了。这样就把可操作步骤从6个降到了3个,其实也就是一个,一种顺序。所以接下来就是选择开始的机器了。。。没法证明啊所以我们就暴力好咯。。。

AC代码:(62ms)

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
vector<int> vv[505];
int a[505];
int in[505];
int bk[505];
int n;
//int solve(int ss) {
//	int ans=n;
//	int cont=0;
//	int now=ss;
//	while(1) {
//		while(1) {
//			int flag=0;
//			for(int i=1; i<=n; i++) {
//				if(in[i]==0&&a[i]==now) {
//					flag=1;
//					in[i]=-1;
//					cont++;
//					for(int j=0; j<vv[i].size(); j++) {
//						int v=vv[i][j];
//						in[v]--;
//					}
//				}
//			}
//			if(flag==0)break;
//		}
//		if(cont==n)break;
//		now++;
//		ans++;
//		if(now==4)now=1;
//	}
//	return ans;
//}
int solve(int st) {queue<int> q;int res = n;for(int i = 1; i<=n; i++) {if(in[i] == 0) q.push(i);}while(!q.empty()) {memset(bk,0,sizeof bk);while(!q.empty()) {int cur = q.front();q.pop();if(bk[cur] == 5) {q.push(cur);break;}bk[cur]++;if(a[cur] != st) {q.push(cur);continue;}int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];in[v]--;if(in[v] == 0) q.push(v);}}		st = st==3?1:st+1;if(!q.empty()) res++;}return res;
}
int temp[15000];
int main() {scanf("%d",&n);memset(in,0,sizeof(in));for(int i=1; i<=n; i++) vv[i].clear();for(int i=1; i<=n; i++) scanf("%d",&a[i]);for(int i=1; i<=n; i++) {int ki;scanf("%d",&ki);for(int j=1; j<=ki; j++) {int x;scanf("%d",&x);vv[x].push_back(i);in[i]++;}}for(int i=1; i<=n; i++) {temp[i]=in[i];}int ans=0x3f3f3f3f;for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(1));for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(2));for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(3));printf("%d\n",ans);
}

AC代码2:(62ms)

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
vector<int> vv[505];
int a[505];
int in[505];
int bk[505];
int n;
int solve(int ss) {int ans=n;int cont=0;int now=ss;while(1) {while(1) {int flag=0;for(int i=1; i<=n; i++) {if(in[i]==0&&a[i]==now) {flag=1;in[i]=-1;cont++;for(int j=0; j<vv[i].size(); j++) {int v=vv[i][j];in[v]--;}}}if(flag==0)break;}if(cont==n)break;now++;ans++;if(now==4)now=1;}return ans;
}
//int solve(int st) {
//	queue<int> q;
//	int res = n;
//	for(int i = 1; i<=n; i++) {
//		if(in[i] == 0) q.push(i);
//	}
//	while(!q.empty()) {
//		memset(bk,0,sizeof bk);
//		while(!q.empty()) {
//			int cur = q.front();q.pop();
//			if(bk[cur] == 1) {
//				q.push(cur);break;
//			}
//			bk[cur]++;
//			if(a[cur] != st) {
//				q.push(cur);continue;
//			}
//			int up = vv[cur].size();
//			for(int i = 0; i<up; i++) {
//				int v = vv[cur][i];
//				in[v]--;
//				if(in[v] == 0) q.push(v);
//			}
//			
//		}		
//		st = st==3?1:st+1;
//		if(!q.empty()) res++;
//	}
//	return res;
//}
int temp[15000];
int main() {scanf("%d",&n);memset(in,0,sizeof(in));for(int i=1; i<=n; i++) vv[i].clear();for(int i=1; i<=n; i++) scanf("%d",&a[i]);for(int i=1; i<=n; i++) {int ki;scanf("%d",&ki);for(int j=1; j<=ki; j++) {int x;scanf("%d",&x);vv[x].push_back(i);in[i]++;}}for(int i=1; i<=n; i++) {temp[i]=in[i];}int ans=0x3f3f3f3f;for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(1));for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(2));for(int i=1; i<=n; i++) in[i]=temp[i];ans=min(ans,solve(3));printf("%d\n",ans);
}

总结:

  拓扑排序最好还是按照下面这个去写,因为AC代码1那里我本来是if(bk[cur]==1)break; 的,然后WA了,后来想想发现确实是这样,并且其实我这样写也不太对,应该是if(bk[cur] == n)break;或者==100。因为标准的拓扑排序是只有一层while的。但是这里为了还有转移到别的机器的过程,所以这里用了两个while,,,所以一定要保证当前机器的已经全部处理完。所以不能 :只要出现过重复的,就说明筛过一遍了,就break。这样是不对的啊,因为遍历到a,,然后a在当前机器但是in[a]!=0,所以bk[a]=1并且将a再次入队。万一筛的过程中,将in[a]减成0了,也就是a可以被执行了,但是我们bk[a]==1了所以就break了。。。所以这是不对的。为了确保是 确实没有一个可以操作的了,应该是入队n次 则break这样。、、、但是这里写的是入队5次,,也过了。

 

首先你要知道啊这题算时间是分成两部分来算的,这也是简化问题的一个方式,把要求的答案分成几份,先把已知答案求出来,然后光去搞未知的就可以了。其实要是不这么写的话也不难写,但是就是思路方面更推荐这一种方法比较优秀。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php$this-conn可以不先定义吗,CodeIgniter 是不是支持PDO 查询?还是本来就不支持

CodeIgniter 是否支持PDO 查询&#xff1f;还是本来就不支持&#xff1f;本帖最后由 default7 于 2014-11-15 19:34:55 编辑配置CodeIgniter 的database 连接方式为PDO 类型&#xff0c;但是怎么都查询不到数据&#xff0c;却可以查处有多少记录&#xff01;自带代码仔细跟踪代…

【ZOJ - 2836 】Number Puzzle (容斥原理)

题干&#xff1a; Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list. Input The input contains several test cases. …

轮番滑动PHP,touch事件之滑动判断(左右上下方向)

touch事件之滑动判断(左右上下方向)作者: 2020.03.11 本文发布于18天前 分类:$("body").on("touchstart", function(e) {// 判断默认行为是否可以被禁用if (e.cancelable) {// 判断默认行为是否已经被禁用if (!e.defaultPrevented) {e.preventDefault();}}…

【CodeForces - 215A】Bicycle Chain (水题)

题干&#xff1a; Vasyas bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th sta…

ubuntu 在线安装php,ubuntu在线安装LNMP

一直以来个人安装lamp环境都是源码编译的&#xff0c;这个过程呢其实也要去经历的&#xff0c;但是毕竟占用时间久&#xff0c;有些时候在做一些测试环境的时候&#xff0c;可以在线安装比较快源码编译nginx可看往期&#xff1a;Nginx的安装对于lnmp的在线安装&#xff0c;如下…

【CodeForces - 215B 】Olympic Medal (数学,公式推导)

题干&#xff1a; The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1)made of metal with density p1 g/cm3. The second part is an i…

oracle的分支语句,oracle中的分支与循环语句

分支语句 if的三种写法一, if 2 < 1 thendbms_output.put_line(‘条件成立‘);end if;二, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsedbms_output.put_line(‘条件不成立‘);end if;三, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsif 4 > 3 …

【CodeForces - 215C 】Crosses (思维,图形题)

题干&#xff1a; There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and co…

Scaffold php,GitHub - yiiplus/scaffold: scaffold是一个基于Yii2高级项目模版工程化实现的应用程序...

Yii 2 Scaffold Project Kit易加-脚手架(scaffold)是一个基于Yii2高级项目模版工程化实现的应用程序&#xff0c;它将更加高效、规范和工程化的满足项目开发的需求。DIRECTORY STRUCTUREcommonconfig/ contains shared configurationsmail/ contains view files for e-mailsmod…

*【CodeForces - 214D 】Numbers (dp,组合数学)

题干&#xff1a; Furik loves writing all sorts of problems, especially such that he cant solve himself. Youve got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it. There is integer n and array a, consisting of ten integers…

oracle修改某个数据类型,Oracle 修改某个字段的数据类型三种方式

1.将该列设置为null,再修改其类型(这样会丢失数据)2.最简单的方法&#xff1a;假设你的表名为 tab_targetcreate table test as select * from tab_target whre 12;alter table test modify (col_name number(5));insert into test select * from tab_target;drop table tab_t…

【EOJ Monthly 2018.10 - B】 莫干山奇遇 (思维构造,数学,数组,贪心)(总结)

题干&#xff1a; Time limit per test: 2.0 seconds Memory limit: 512 megabytes 出题人当然是希望出的题目有关 oxx&#xff0c;于是想方设法给题目配上一些有关 oxx 的背景故事&#xff0c;使得它看起来不那么无趣。但有的时候却无法引入合适的小姐姐&#xff0c;使得 o…

有奶瓶的linux系统,用U盘启动BEINI(奶瓶)系统

用U盘启动&#xff1a;奶瓶(beini)这个系统&#xff0c;是一款基于Tiny Core Linux 搭建的无线网络安全测试系统&#xff0c;当然由于它是用来安全测试的系统&#xff0c;因此在安全方面自然有着强大的功能。而且&#xff0c;这个系统非常简便易学&#xff0c;因此现在已经逐渐…

【CodeForces - 227A】Where do I Turn? (计算几何,叉积判断直线拐向)

题干&#xff1a; Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled at point C and began to terrorize the residents of the surrounding villages. A brave hero decided to put an end to the dragon. He moved from…

linux内核镜像sd卡,【原创】Linux QT镜像的制作--制作SD卡启动盘

最近买了个新的开发板&#xff0c;原生的是Android操作系统&#xff0c;需要自己少个启动盘&#xff0c;制作LinuxQT操作系统。新的开发板带这个制作的源文件&#xff0c;要先把这个文件拷贝到虚拟机Ubunbtu的共享目录下。打开share文件下显示文件如下&#xff1a;打开文件夹命…

【CodeForces - 227B 】Effective Approach (STL,思维)

题干&#xff1a; Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array. According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the numbe…

linux信号值头文件位置,Linux C 信号处理机制

一 . 信号1. 信号&#xff1a;是内核发送给某一进程的一种消息 。2. 信号机制&#xff1a;是Linux系统中用于进程之间相互通信或操作的一种机制。3. 信号的来源&#xff1a;信号来源于内核4. 产生原因&#xff1a; (1)用户通过终端输入 (2)进程执行(3)一个进程调用kill向另一个…

【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)

题干&#xff1a; This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into num…

简述linux系统的安全性,Linux操作系统的安全性有哪些过人之处

01用户/文件权限Linux的敲门砖Linux操作系统的安全性是有目共睹的&#xff0c;相比Windows操作系统&#xff0c;到底Linux有哪些过人之处&#xff1f;这里我们就抛砖引玉&#xff0c;挑选三点重要的特点给大家说明&#xff0c;为什么说Linux操作系统安全性有其他系统无可比拟的…

【qduoj - 夏季学期创新题】C语言课程设计-阶梯问题(dp,高精度大数)

题干&#xff1a; 描述 N级阶梯&#xff0c;人可以一步走一级&#xff0c;也可以一步走两级&#xff0c;求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n&#xff0c;代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法&#xff0c;输出结…