poj 1276
背包问题的转化~~~
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <cstring> 12 #include <queue> 13 #include <cstdio> 14 #define CLR(arr, what) memset(arr, what, sizeof(arr)) 15 typedef long long ll; 16 const int MAX = 100005; 17 const int maxc = 1500; 18 int top = 0xFFFFFFFF; 19 using namespace std; 20 int val[maxc]; 21 int rn; 22 void add(int numvar, int valvar) { 23 int i = 0; 24 int orinum = numvar; 25 while (numvar) { 26 val[rn] = (valvar << i); 27 numvar = numvar & (~(1 << i)); 28 if (0 == numvar) { //最后一位单独处理 29 orinum = orinum - ((1 << i) - 1); 30 val[rn] = valvar * orinum; 31 } 32 rn++; 33 i++; 34 } 35 } 36 int dp[MAX]; 37 38 int main() { 39 int cash, t1, t2, n, cur, next; 40 while (scanf("%d%d", &cash, &n) != EOF) { 41 rn = 0; 42 CLR(dp, 0); 43 dp[0] = 1; 44 for (int i = 0; i < n; i++) { 45 scanf("%d%d", &t1, &t2); 46 add(t1, t2); 47 } 48 for (int i = 0; i < rn; i++) { 49 for (int j = cash; j >= 0; j--) { 50 cur = dp[j]; 51 if (cur == 1) { 52 next = j + val[i]; 53 if (next <= cash) { 54 dp[next] = 1; 55 } 56 } 57 } 58 } 59 for (int i = cash; i >= 0; i--) { 60 if (dp[i]) { 61 cout << i << endl; 62 break; 63 } 64 } 65 66 } 67 return 0; 68 }