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