http://acm.beihua.edu.cn/problem/1007

Tom and Bag
 

Description

Tom is the most handsome CCPC contestant in HIT.Tom got a bag recently. The bag has a volume V. Tom has n boxes of candy. He wants to fillthis bag with these boxes.Each box i has a value Ai, a volume Vi, and a color Ci.We define a bag’s beautiful level P as the number of different colors on boxes in this bag.Tom wants to make P not less than K to make the bag colorful.After that, Tom wants to make the sum of values of boxes in the bag maximum. The sum ofvolumes of boxes in the bag could not exceed V.It is guaranteed that Tom could use these n boxes to make the bag colorful.Tom wants you to help him calculate what’s the maximum sum of values his colorful bagcould carry.

Input

First line contains an integer T (1 ≤ T ≤ 5), represents there are T test cases.For each test case:The first line contains three integers N, K, V (1 ≤ N ≤ 100, 1 ≤ K ≤ 5, 1 ≤ V ≤200), represents there are N boxes, the number of colors could not be less than K, and the bag’svolume is V.Each line of the following N lines contains three integers Ai, Vi, Ci (1 ≤ Ai ≤ 1000, 1 ≤Vi ≤ V, 1 ≤ Ci ≤ N), represents the ith box’s value, volume and color.

Output

For each test case, output one line with an integer W, represents the maximum sum ofvalues.

Sample Input 1

3
5 1 10
3 2 1
5 8 1
7 9 1
1 2 2
2 2 3
5 2 10
3 2 1
5 8 1
7 9 1
1 2 2
2 2 3
5 3 10
3 2 1
5 8 1
7 9 1
1 2 2
2 2 3

Sample Output 1

8
7
6

Hint

Tom will eat all candies couldn’t be filled into the bag. So don’tworry about wasting candies.

题意:有一个容量为v的背包,有n个物品,每种物品都有一个价值ai,一个体积 vi,一种颜色 ci,求放入背包的颜色数至少为k的背包最大价值

题解:比平常的背包就多了一个颜色的限制,可以通过按颜色排序,让颜色一样的在一块,dp[i][j][k]表示到第i种物品的时候已经有了j种颜色使用容积为k时的背包价值最大值,在dp更新的过程记录一下当前值是否是由该种颜色的值更新的,即分类更新一下使用颜色的那一维即可

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[][][],flag[][][];
struct pot{
int a;
int va;
int ca;
}p[];
bool cmp(struct pot aa,struct pot bb){
return aa.ca<bb.ca;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,k,v;
scanf("%d%d%d",&n,&k,&v);
for(int i=;i<=n;i++){
scanf("%d%d%d",&p[i].a,&p[i].va,&p[i].ca);
}
sort(p+,p++n,cmp);
int c=-;
memset(dp,-,sizeof(dp));
for(int i=;i<=n;i++){
dp[i][][]=;
}
int ans=;
for(int i=;i<=n;i++){
for(int j=;j<=v;j++){
for(int q=;q<=n;q++){
dp[i][q][j]=dp[i-][q][j];
flag[i][q][j]=flag[i-][q][j];
if(j-p[i].va>=&&flag[i-][q-][j-p[i].va]!=p[i].ca){
if(dp[i-][q-][j-p[i].va]!=-&&dp[i-][q-][j-p[i].va]+p[i].a>=dp[i][q][j]){
dp[i][q][j]=dp[i-][q-][j-p[i].va]+p[i].a;
flag[i][q][j]=p[i].ca;
}
}
if(j-p[i].va>=&&flag[i-][q][j-p[i].va]==p[i].ca){
if(dp[i-][q][j-p[i].va]!=-&&dp[i-][q][j-p[i].va]+p[i].a>=dp[i][q][j]){
dp[i][q][j]=dp[i-][q][j-p[i].va]+p[i].a;
flag[i][q][j]=p[i].ca;
}
}
if(q>=k){
ans=max(ans,dp[i][q][j]);
}
}
}
}
printf("%d\n",ans);
}
return ;
}

[Tom and Bag][需要记录过程的dp]的更多相关文章

  1. ImportError: No module named tornado.ioloop 记录过程

    ImportError: No module named tornado.ioloop 记录过程 安装 pycurl    pip install pycurl 报错 'curl-config' no ...

  2. 【Oracle RAC】Linux系统Oracle18c RAC安装配置详细记录过程(图文并茂)

    本文Oracle 18c GI/RAC on Oracle Linux step-by-step 的安装配置步骤,同时也包含dbca 创建数据库的过程. 1. 关闭SELINUX,防火墙vi /etc ...

  3. 【学习笔记&训练记录】数位DP

    数位DP,即对数位进行拆分,利用数位来转移的一种DP,一般采用记忆化搜索,或者是先预处理再进行转移 一个比较大略的思想就是可以对于给定的大数,进行按数位进行固定来转移记录答案 区间类型的,可以考虑前缀 ...

  4. code forces 148D Bag of mice (概率DP)

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 经测试稳定可用的蓝牙链接通信Demo,记录过程中遇到的问题的思考和解决办法,并整理后给出一个Utils类可以简单调用来实现蓝牙功能

    说明:这是本人在蓝牙开发过程中遇到过的问题记录和分析,以及解决办法. 在研究过程中,许多的前人给出的解决方案和思路指导对我相当有帮助,但并非都是可采取的解决方法, 经过本人对这些方法的测试和使用过后, ...

  6. (最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160

    http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Other ...

  7. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  8. CF 148D Bag of mice【概率DP】

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes Promblem descriptio ...

  9. CF148D. Bag of mice(概率DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. 自动调参库hyperopt+lightgbm 调参demo

    在此之前,调参要么网格调参,要么随机调参,要么肉眼调参.虽然调参到一定程度,进步有限,但仍然很耗精力. 自动调参库hyperopt可用tpe算法自动调参,实测强于随机调参. hyperopt 需要自己 ...

  2. 类 Arrays StringBuilder 跟 StringBuffer 的异同 SimpleDateFormat

    类 String 同:起连接字符串类型作用 异: StringBuffer    //线程安全  效率慢 StringBuilder   //线程不安全  效率快 类 Arrays copyOf  ( ...

  3. StringBuffer&StringBuilder

    对字符串修改时,用到StringBuffer&StringBuilder,能够多次修改对象并且不产生新的未使用对象 StringBuilder线程不安全(不能同步访问),速度有优势,多数情况下 ...

  4. java中的常用特殊字符

    1.转义字符反斜杠(\) 我们知道html中大都是双标签,如果在标签内想要输出带有标签结束符的文本都必须进行转义,html中是采用对应的字符替换,如<可用<替换 在java当中,我们要转义 ...

  5. Problem A: 重载字符的加减法

    Description 定义一个字符类Character,只有一个char类型的数据成员. 重载它的+.-.<<和>>运算符,其中+.-的第二个操作数是int类型的整数n.“+ ...

  6. SharePoint Framework解决方案管理参考(一)

    博客地址:http://blog.csdn.net/FoxDave 使用SPFx,你的企业可以轻松构建解决方案跟Office 365和SharePoint Online集成.SPFx解决方案基于现代w ...

  7. better-scroll

    better-scroll会将默认事件阻止掉,如果自己写的部分需要有点击事件,需要在参数里加上click:true. 同时,在PC上或某些手机端,由于未成功将touchend事件move掉,点击事件会 ...

  8. code about led_shake

    //write by:cyt //Project Name:Led on/off //Time:2017-2-10 #include<reg51.h> void delay(int c) ...

  9. Linux虚拟机部署单机solr报错500解决方法之一

    HTTP Status 500 - {msg=SolrCore 'collection1' is not available due to init failure: Could not load c ...

  10. php中生成透明背景png缩略图程序

    /** *$sourePic:原图路径 * $smallFileName:小图名称 * $width:小图宽 * $heigh:小图高 */function pngthumb($sourePic,$s ...