标签:
Input
Output
Sample Input
Sample Output
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<string> 5 #define maxn 500+5 6 using namespace std; 7 int main() 8 { 9 int T; 10 cin >> T; 11 while (T--) 12 { 13 int wight[maxn], W[maxn],pr[maxn]; 14 int n, m, nl,p,h,c,cl=0; 15 cin >> n >> m; 16 for (int i = 0; i < m; i++) 17 { 18 cin >> p >> h >> c; 19 cl += c; 20 if (i == 0) 21 { 22 for (int j = 0; j < c; j++) 23 pr[j] = p, wight[j] = h; 24 nl = cl; 25 } 26 else 27 { 28 for (int k = nl; k < cl; k++) 29 pr[k] = p, wight[k] = h; 30 nl = cl; 31 } //以上的一大段都在把大米分堆赋值 32 } 33 34 for (int i = 0; i <=cl; i++) 35 { 36 W[i] = 0; 37 } 38 for (int i = 0; i <cl; i++) 39 { 40 for (int j = n; j >= pr[i]; j--) 41 { 42 W[j] = max(W[j], W[j - pr[i]] + wight[i]); 43 } 44 } 45 cout << W[n] << endl; 46 } 47 return 0; 48 }
心得:
提交的时候一直错误:
原因如下:
1、数组开小了,之前开的是500+5
2、用memset()给W[]赋值为0了,原来不能这样做,具体原因我也不晓得23333333
3、给W初始化的时候,循环要=cl。嗯!
一路坎坷,终于过了~~~~~~泪奔会儿。。。o(>_<)o ~~
标签:
原文地址:http://www.cnblogs.com/Lynn0814/p/4726726.html