1 //Accepted 896 KB 156 ms 2 //http://blog.csdn.net/juststeps/article/details/8712150 3 //dp[i][l]=max(dp[i][l],dp[i][l-v[i][j].weight]+v[i][j].value);第i种已经取数后用v[i][j]更新 4 //dp[i][l]=max(dp[i][l],dp[i-1][l-v[i][j].weight]+v[i][j].value);第i种还没有取数用v[i][j]更新 5 //显然,后一种情况应该后更新 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <queue> 10 #include <cmath> 11 #include <vector> 12 #include <algorithm> 13 using namespace std; 14 /** 15 * This is a documentation comment block 16 * 如果有一天你坚持不下去了,就想想你为什么走到这儿! 17 * @authr songt 18 */ 19 const int imax_n = 15; 20 const int imax_v = 10005; 21 struct node 22 { 23 int weight,value; 24 }; 25 vector<node > vec[imax_n]; 26 int dp[imax_n][imax_v]; 27 int n,v; 28 int k; 29 int max(int a,int b) 30 { 31 return a>b?a:b; 32 } 33 void Dp() 34 { 35 memset(dp,-1,sizeof(dp)); 36 //for (int i=0;i<=k;i++) 37 //for (int j=0;j<=v;j++) 38 //dp[i][j]=-1; 39 for (int i=0;i<=v;i++) 40 { 41 dp[0][i]=0; 42 } 43 for (int i=1;i<=k;i++) 44 { 45 for (int j=0;j<vec[i].size();j++) 46 { 47 for (int l=v;l>=vec[i][j].weight;l--) 48 { 49 if (dp[i][l-vec[i][j].weight]!=-1) 50 { 51 dp[i][l]=max(dp[i][l],dp[i][l-vec[i][j].weight]+vec[i][j].value); 52 } 53 if (dp[i-1][l-vec[i][j].weight]!=-1) 54 { 55 dp[i][l]=max(dp[i][l],dp[i-1][l-vec[i][j].weight]+vec[i][j].value); 56 } 57 } 58 } 59 } 60 if (dp[k][v]==-1) 61 { 62 printf("Impossible\n"); 63 } 64 else 65 { 66 printf("%d\n",dp[k][v]); 67 } 68 } 69 int main() 70 { 71 while (scanf("%d%d%d",&n,&v,&k)!=EOF) 72 { 73 int kind; 74 node x; 75 for (int i=1;i<=10;i++) 76 vec[i].clear(); 77 for (int i=1;i<=n;i++) 78 { 79 scanf("%d%d%d",&kind,&x.weight,&x.value); 80 vec[kind].push_back(x); 81 } 82 Dp(); 83 } 84 return 0; 85 }
时间: 2024-10-12 20:11:42