ZOJ3554 A Miser Boss(dp)

本文介绍了一个加工任务调度问题,通过使用动态规划结合bitset优化的方法来解决。目标是在满足所有机床连续工作且同时结束工作的条件下,寻找是否存在一种安排使得加工所有工件的时间最短。

转载请注明出处: 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 }

 

转载于:https://www.cnblogs.com/fraud/p/4733479.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值