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. K2签约龙光地产,为集团实现“千亿目标”保驾护航

    随着房地产行业步入成熟期,行业整合及转型速度变快,房企要在数字经济的背景下实现稳步发展,企业信息化建设是其中的重要一环.此次龙光地产选择与K2携手,用统一流程平台为集团保驾护航,向实现千亿目标迈进. ...

  2. Oracle提取中文字符串拼音首字母函数

    通过oracle的NLSSORT函数对汉字按照拼音排序,然后根据汉字的区间返回对应的首字母. 效果1,获取拼音简码: 效果2,获取姓名首字母: 创建函数: /* 获取拼音简码函数 */ CREATE ...

  3. Buzzsumo大型教程(内容营销+外链outreach必备)营销神器

    做内容营销,Buzzsumo基本是必备工具.做谷歌白帽SEO的百分八十应该都用过或者至少接触过.在国外就更不用说了,很多网络营销大牛眼里,Buzzsumo的重要程度绝对超过Ahrefs! Buzzsu ...

  4. MFC中创建自定义消息

    消息映射.循环机制是Windows程序运行的基本方式.VC++ MFC 中有许多现成的消息句柄,可当我们需要完成其它的任务,需要自定义消息,就遇到了一些困难.在MFC ClassWizard中不允许添 ...

  5. python orm框架

    #!/usr/bin/python# -*- coding: utf-8 -*-from sqlalchemy import create_enginefrom sqlalchemy import T ...

  6. 《JavaScript Dom 编程艺术》读书笔记-第10章

    用JS实现动画~内容包括: 1. 动画基础知识 2. 用动画丰富网页的浏览效果 动画就是让元素的位置随时间而不断变化. 位置: //CSSelement{ position:absolute; top ...

  7. P3954 成绩(noip2017普及组)

    题目描述 牛牛最近学习了C++入门课程,这门课程的总成绩计算方法是: 总成绩=作业成绩\times 20\%+×20%+小测成绩×30\%+×30%+期末考试成绩\times 50\%×50% 牛牛想 ...

  8. PHP安装过程中问题详解

    安装Apace时我就犯了一个大错误.因为我的母语是JAVA,我以为Tomcat就是Apache.其实不然,Tomcat是给Java用的,处理JSP等的动态页面. 而PHP则是单纯的用Apache安装A ...

  9. 原生JS和jQuery操作DOM的区别小结

    一.Js原生对象和jQuery实例对象的相互转化: (1).原生JS对象转JQ对象: $(DOM对象); (2). JQ对象转原生JS对象: $(DOM对象).get(index); //注意区分eq ...

  10. wpf 命令

    上图为命令的 示例