度度熊的午饭时光
度度熊的午饭时光
Accepts: 823
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Case #1:
34 29
2 3 5 6
Case #2:
0 0
一开始看到题目那么多要求,觉得做不出来,结果我没考虑那么多,就直接写了,用了01背包的写法,然后
回朔求解。
1 #include <bits/stdc++.h> //百度之星1004 2 #define mem(a) memset(a,0,sizeof(a)) 3 using namespace std; 4 struct Node{ 5 int price,sco; 6 }; 7 Node node[105]; 8 int dp[1005]; 9 bool vis[105][1005]; 10 bool ans[105]; 11 int main() { 12 int n; 13 scanf("%d", &n); 14 for (int l = 1; l <= n; l++) { 15 int B, N; 16 mem(node); 17 scanf("%d%d", &B, &N); 18 for (int j = 1; j <= N; j++) { 19 scanf("%d", &node[j].sco); 20 scanf("%d", &node[j].price); 21 } 22 mem(dp); 23 mem(ans); 24 memset(vis, false, sizeof(vis)); 25 for (int k = 1; k <= N; k++) { 26 for (int j = B; j >= node[k].price; j--) { 27 if (dp[j] < (dp[j - node[k].price] + node[k].sco)) { 28 dp[j] = dp[j - node[k].price] + node[k].sco; 29 vis[k][j] = true; 30 } else 31 continue; 32 } 33 } 34 int p = B, cnt = 0; 35 for (int i = N; i >= 1; i--) { 36 if (vis[i][p]) { 37 ans[i] = true; 38 p -= node[i].price; 39 cnt++; 40 } 41 } 42 43 int Ans = 0; 44 for (int i = 1; i <= N; i++) { 45 if (ans[i]) 46 Ans += node[i].price; 47 } 48 49 printf("Case #%d:\n", l); 50 printf("%d %d\n", dp[B], Ans); 51 for (int i = 1; i <= N; i++) 52 if (ans[i]) 53 printf("%d%c", i, (--cnt == 0) ? '\n' : ' '); 54 } 55 return 0; 56 }