ACM思维题训练集合

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

2-3 3-1 还有就是 3-2 2-1 1-3这种,一种是1另一种是2,所以我们不走1-3这种用两段1-2 2-3代替花费相同,这样在进行拓扑排序完事了。

吐槽一下数据思路错了也能过。

后来想了一下如果一开始三台电脑都能开始一个工作,那么先从哪台开始呢,不知道,所以三台为起始点进行拓扑选最小的的答案输出。

#include <bits/stdc++.h>
using namespace std;
vector<int> mp[15000];
int d[5][5], a[250], deg[250], temp[205], n;
int tooper(int ss)
{
queue<int> s;
int ans = n, cnt = 0, now = ss;
while (1)
{
while (1)
{
int flag = 0;
for (int i = 1; i <= n; ++i)
{
if (deg[i] == 0 && a[i] == now)
{
flag = 1;
deg[i] = -1;
cnt++;
for (int j = 0; j < mp[i].size(); ++j)
{
int v = mp[i][j];
deg[v]--;
}
}
}
if (flag == 0)
break;
}
if (cnt == n)
break;
now++;
ans++;
now = (now == 4 ? 1 : now);
}
return ans;
} int main()
{
d[1][1] = d[2][2] = d[3][3] = 0;
d[1][2] = d[2][3] = d[3][1] = 1;
d[2][1] = d[3][2] = d[1][3] = 0x3f3f3f3f;
cin>>n;
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)
{
int k;
scanf("%d", &k);
for (int j = 1; j <= k; ++j)
{
int x;
scanf("%d", &x);
mp[x].push_back(i);
deg[i]++;
}
}
for (int i = 1; i <= n; ++i)
temp[i] = deg[i];
int ans = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i)
deg[i] = temp[i];
ans = min(ans, tooper(1));
for (int i = 1; i <= n; ++i)
deg[i] = temp[i];
ans = min(ans, tooper(2));
for (int i = 1; i <= n; ++i)
deg[i] = temp[i];
ans = min(ans, tooper(3));
printf("%d\n", ans);
}

最新文章

  1. 将asp.net core站点发布到IIS上遇到的问题
  2. js确认删除对话框
  3. 【JavaEE】SSH+Spring Security基础上配置AOP+log4j
  4. C语言和数据结构的书单-再次推荐
  5. php获取文件创建时间、修改时间
  6. 装载:对CSS中的Position、Float属性的一些深入探讨
  7. linux下配置双网卡及RAC规划——1
  8. c# GridView Footor列求合计
  9. JS参数使用带参数的方法
  10. BZOJ 1072 [SCOI2007]安排perm 如压力DP
  11. WP8.1开发中关于媒体(图片)文件的生成操作,属性如何设置(内容/嵌入资源等);
  12. flask 上传头像
  13. 消息队列比较-rabbitmq/kafka/rocketmq/ONS
  14. 【转】深入分析 Parquet 列式存储格式
  15. ldap集成bitbucket
  16. Ubuntu配置ORB-SLAM2过程中的问题
  17. Eclipse的智能提示的设置
  18. Java开发工程师面试题1
  19. 7-4素数环 uva 524
  20. nginx负载均衡的5种策略

热门文章

  1. django禁用csrf
  2. linux中的隐藏权限,chattr,lsattr
  3. Python 1基础语法一(注释、行与缩进、多行语句、空行和代码组)
  4. win10+ubuntu双系统修复ubuntu启动引导
  5. tf.nn.bias_add 激活函数
  6. 做一个通过dockerfile从零构建centos7.4
  7. Linux 下迁移 Nexus3
  8. 莫名的证书错误...ERROR ITMS-90035:&quot;Invalid Signature.
  9. Go gRPC进阶-go-grpc-middleware使用(八)
  10. 微信小程序画布(1)