ZOJ3554 A Miser Boss(dp)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

A Miser Boss


Time Limit: 2 Seconds      Memory Limit: 65536 KB


There are three different lathes in a factory namely A,B,C. They are all able to work on all kinds of workpieces. And there are n workpieces to be processed, denoted by 1,2,..,n.

Due to different internal implemetations, for a specific workpiece i, it costs A a[i] seconds to finish the job on it while B takes b[i] and C takes c[i] seconds. Each lathe can work on at most one workpiece a time.

Additionally, the Boss is so stingy that he didn‘t want his lathes to be disengaged at any time before the end of the work. That is:
1. there should not be a time when one of the lathes is in leisure. 
2. all lathes should stop simultaneously,and start from time 0.

Your task is to find if there exists an arrangement meeting with the constraints above. if there is,output the minimal time needed to process all the workpieces. Output a line NO else.

Input

There are multiple test cases. The first line of each case is an integer N(0 < N ≤ 40 ) , followed by N lines. In the following N lines, line i contains three integer,a[i],b[i],c[i] (0 < a[i],b[i],c[i] ≤ 120 && 0 < sum(a[i]),sum(b[i]),sum(c[i]) ≤ 120 ) indicating the seconds A,B,C takes to process workpiece i.

Output

Output one line the minimal seconds it takes to process all workpieces within the constraints if there is an arrangement. Print NO if not.

Sample Input

3
7 1 2
1 7 1
1 3 7
2
1 2 3
3 2 1

Sample Output

1
NO

恕我愚钝,只能想到n*120*120*120的方法,dp[i][j][k][l]表示取到第i个时A取的总和是j,B是k,C是l这种方案是否可行

既然只要判断是否可行,那么其实第四维可以优化。于是我想到可以利用bitset,每次只要或一下就行,嗯,就是这么简单粗暴

 1 /**
 2  * code generated by JHelper
 3  * More info: https://github.com/AlexeyDmitriev/JHelper
 4  * @author xyiyy @https://github.com/xyiyy
 5  */
 6
 7 #include <iostream>
 8 #include <fstream>
 9
10 //#####################
11 //Author:fraud
12 //Blog: http://www.cnblogs.com/fraud/
13 //#####################
14 //#pragma comment(linker, "/STACK:102400000,102400000")
15 #include <iostream>
16 #include <sstream>
17 #include <ios>
18 #include <iomanip>
19 #include <functional>
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <queue>
25 #include <deque>
26 #include <stack>
27 #include <set>
28 #include <map>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cmath>
32 #include <cstring>
33 #include <climits>
34 #include <cctype>
35
36 using namespace std;
37 #define rep(X, N) for(int X=0;X<N;X++)
38 #define rep2(X, L, R) for(int X=L;X<=R;X++)
39
40 int a[110], b[110], c[110];
41
42 #include <bitset>
43
44 bitset<121> dp[42][121][121];
45
46 class TaskF {
47 public:
48     void solve(std::istream &in, std::ostream &out) {
49         int n;
50         while (in >> n) {
51             int tot = 0;
52             rep2(i, 1, n) {
53                 in >> a[i] >> b[i] >> c[i];
54                 tot += c[i];
55             }
56             rep(i, n + 1)
57                 rep(j, 121)
58                     rep(k, 121)dp[i][j][k].reset();
59             int ta = 0, tb = 0;
60             dp[0][0][0][0] = 1;
61             rep2(i, 1, n) {
62                 rep2(j, 0, ta) {
63                     rep2(k, 0, tb) {
64                         dp[i][j + a[i]][k] |= dp[i - 1][j][k];
65                         dp[i][j][k + b[i]] |= dp[i - 1][j][k];
66                         dp[i][j][k] |= (dp[i - 1][j][k] << c[i]);
67                     }
68                 }
69                 ta += a[i];
70                 tb += b[i];
71             }
72             int ans = 0;
73             rep2(i, 1, 120) {
74                 if (dp[n][i][i][i]) {
75                     ans = i;
76                     break;
77                 }
78             }
79             if (ans)out << ans << endl;
80             else out << "NO" << endl;
81         }
82     }
83 };
84
85 int main() {
86     std::ios::sync_with_stdio(false);
87     std::cin.tie(0);
88     TaskF solver;
89     std::istream &in(std::cin);
90     std::ostream &out(std::cout);
91     solver.solve(in, out);
92     return 0;
93 }
时间: 2024-12-26 11:37:28

ZOJ3554 A Miser Boss(dp)的相关文章

关于abstract中遇到的问题

所做东西的需求: 定义英雄类和Boss类,创建3个英雄对象,分别拿不同的武器,对敌人循环回合制攻击,输出战斗过程. 英雄类:特征:HP.MP.类型(枚举类型:弓箭手.法师.骑士).武器 行为:攻击.防御 Boss类:特征:HP.攻击力.防御力 行为:攻击 using System; using System.Collections; using System.Collections.Generic; namespace Exam { /// <summary> /// 根据武器类型判断是否能进

UVA-01220 Party at Hali-Bula (树形DP+map)

题目链接:https://vjudge.net/problem/UVA-1220 思路: 树形DP模板题,求最大人数很简单,难点在于如何判断最大人数的名单是否有不同的情况: 解决方法是用一个数组f[manx][2]记录该节点是否出场的情况,为真时代表有多种情况; 具体讨论: 当父节点的值加上某个子节点的值时,他的f的情况也和该子节点一样: 当某个节点dp(i, 0) == dp(i, 1), 则该节点以及它的父节点也一定有多种情况(父节点必定取其中之一). Code: 1 #include<bi

概率dp HDU 3853

H - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3853 Appoint description:  System Crawler  (2014-10-22) Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wa

zoj 2297 Survival 状压dp

Description The King of Fighter 97 (KOF97) is an electronic game of wrestling type. Once it was fashionable among youths. The game is amused. However, playing by oneself is not as excited as with friends. Getting bored? Maybe you can alter to the sur

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system. The information department told you that there are n enemy soldiers and their network w

【树形DP】 HDU 2412 Party at Hali-Bula

给出根节点(BOSS) 然后还有N-1个边  A B 由B指向A (B为A 的上司) 每次只能选择这个关系中的其中一个 求最多选几个点 并且输出是不是唯一的 重点判断是否唯一: 1.若下属不去和去都人数一样的话则上司不去的话就不唯一(上司去了下属必定不能去,所以不满足) 2.若下属不去还不唯一的话  上司去了也是不唯一 #include <cstdio> #include <cstdlib> #include <cstring> #include <climits

5.1个人赛解题报告(区间dp,按位与或,图论等水题)

这次5.1打了一场个人赛,已经连赛了三周了,有点疲惫感觉,可能自己太水了,每次都有点小紧张. 这次只解出来三道题,然而有一道按位与按位或的水题不知道思路实在是做题太少,还有就是第一题区间DP,也消耗了不少的时间,但是没有成功的写出来,还是不够熟练啊. 下面写报告 A. System Administrator time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

Hdoj 1520&amp;Poj2342 Anniversary party 【树形DP】

Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5917 Accepted Submission(s): 2692 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural

POJ 3249 拓扑+dp

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 2168 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job