HDU 3033 I love sneakers!(分组背包变形)
参考:http://blog.csdn.net/dgq8211/article/details/7733275
现在 还不是太明白。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstring> 3 #include<vector> 4 #include<cstdio> 5 6 using namespace std; 7 8 struct food 9 { 10 int price,value; 11 }; 12 13 vector < food > Q[15]; 14 int dp[15][10005]; 15 16 int main( ) { 17 18 int n,k,Sum,a,b,c; 19 food t; 20 while(~scanf("%d %d %d",&n,&Sum,&k)) { 21 22 for( int i=1;i<=k;i++) 23 Q[i].clear(); 24 memset(dp,-1,sizeof(dp)); 25 for( int i=0;i<=Sum;i++) 26 dp[0][i]=0; 27 28 for( int i=1;i<=n;i++) { 29 cin >>a >>b >>c; 30 t.price=b; 31 t.value=c; 32 Q[a].push_back(t); 33 } 34 35 for( int i=1;i<=k;i++) 36 for( int j=0;j<(int)Q[i].size();j++){ 37 t=Q[i][j] ; 38 for( int k=Sum;k>=t.price;k--) { 39 if( dp[i][k-t.price]!=-1 ) 40 dp[i][k]=max( dp[i][k], dp[i][k-t.price]+t.value ); 41 if( dp[i-1][k-t.price]!=-1) 42 dp[i][k]=max( dp[i][k], dp[i-1][k-t.price]+t.value ); 43 } 44 } 45 46 47 int Max=-1; 48 for( int i=0;i<=Sum;i++) 49 Max=max( Max, dp[k][i] ); 50 if( Max==-1 ) cout <<"Impossible" <<endl; 51 else cout <<Max <<endl; 52 } 53 54 }