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);
}

CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)的更多相关文章

  1. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

  2. HDU 6073 Matching In Multiplication(拓扑排序+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...

  3. luogu 3441 [POI2006]MET-Subway 拓扑排序+思维

    Description 给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多. Input 第一行两个正整数N.L(2 <= N <= 1,000,000, ...

  4. 2019牛客暑期多校训练营(第五场)H-subsequence 2 (拓扑排序+思维)

    >传送门< 题意: 给你几组样例,给你两个字符a,b,一个长度len,一个长度为len的字符串str,str是字符串s的子串 str是s删掉除过a,b两字符剩下的子串,现在求s,多种情况输 ...

  5. 洛谷 P4017 最大食物链计数 (拓扑排序,思维)

    题意:有\(n\)个点,连\(m\)条边,求最多有多少条食物链(从头走到为有多少条路径). 题解:之前抽了点时间把拓扑排序补完了,这题其实就是一道拓扑排序的裸题.关于拓扑排序: ​ 1.首先,我们用\ ...

  6. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  7. CodeForces - 721C 拓扑排序+dp

    题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c     //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...

  8. 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题

    Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...

  9. Sorting It All Out (拓扑排序+思维)

    An ascending sorted sequence of distinct values is one in which some form of a less-than operator is ...

随机推荐

  1. ssh秘钥免交互批量分发脚本

    将以下内容保存为.sh文件后运行即可,需根据各自情况修改ip_up和ip_arr #!/bin/bash #脚本功能:ssh秘钥免交互批量分发 #制 作 人:罗钢 联系方式:278554547@qqc ...

  2. 关于TD信息树自己的体验和建议

    自己选择的是17级学长13组的TD消息树,通过对这个软件的使用,感觉整体上还是很好的,他的主要功能就相当于把微信的朋友圈还有qq的好友动态等功能集合到了一起.这个软件整体就相当于一个空间,可以加好友互 ...

  3. 修复Windows10引导,适用gpt+uefi环境

    在双硬盘多系统安装时,容易损坏Win10的开机引导文件. 可以尝试用Windows原版安装盘启动,进入命令提示符模式: 首先使用diskpart命令确认需要修复的Windows分区的安装卷X:. 再运 ...

  4. Linq下有一个非常实用的SelectMany方法,很多人却不会用

    在平时开发中经常会看到有些朋友或者同事在写代码时会充斥着各种for,foreach,这种程式代码太多的话阅读性特别差,而且还显得特别累赘,其实在FCL中有很多帮助我们提高阅读感的方法,而现实中很多人不 ...

  5. webpack配置示例

    var webpack = require('webpack'); var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('commo ...

  6. .net 后台调用前台JS函数

    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('上 ...

  7. Deep Dream模型与实现

    Deep Dream是谷歌公司在2015年公布的一项有趣的技术.在训练好的卷积神经网络中,只需要设定几个参数,就可以通过这项技术生成一张图像. 本文章的代码和图片都放在我的github上,想实现本文代 ...

  8. C++线性表的链式存储结构

    C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...

  9. 一、华为模拟器eNSP下载与安装教程

    简单介绍一下 eNSP: eNSP是一款由华为提供的免费的图形化网络仿真工具平台,它将完美呈现真实设备实景(包括华为最新的ARG3路由器和X7系列的交换机),支持大型网络模拟,让你有机会在没有真实设备 ...

  10. Web开发与设计之Google兵器谱-Web开发与设计利器

    Web开发与设计之Google兵器谱-Web开发与设计利器 博客分类: Java综合 WebGoogleAjaxChromeGWT 笔者是个Java爱好者也是用Java进行web开发的工作者.平时笔者 ...