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. HFun.快速开发平台(五)=》自定义系统数据选择

    本篇介绍HFun.快速开发平台的另一项系统常用功能:系统数据或参数选择,主要应用在表单录入中信息的选择,如类别,编号等.先贴出本系统实现的页面效果: 如上图所示,系统中将参数的选择统一展现为该方式,开 ...

  2. L1-059 敲笨钟

    微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.为了增加敲钟的趣味性,还会糟改几句古诗词.其糟改的方法为:去网上搜寻压“ong”韵的古诗词,把句尾的三个字换成“敲笨钟”.例如唐代 ...

  3. CentOS6.5 - linux在虚拟机连接主机(使用nat)

    NAT模式:是虚拟系统借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网.也就是说,使用NAT模式可以实现在虚拟系统里访问互联网. NAT模式下的虚拟系统的TCP/IP配置信息是由VMn ...

  4. ionic 实现仿苹果手机通讯录搜索功能

    前言 由于之前做的SRM移动应用采用的是ionic1.x框架进行开发的,由于性能方便的诟病,导致用户体验不是很友好,于是想着怎么样去提高应用的性能问题.此时刚好ionic3.x框架已发布,由于ioni ...

  5. Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?) (分解质因数)

    题目:http://codeforces.com/problemset/problem/1114/C 题意:给你n,m,让你求n!换算成m进制的末尾0的个数是多少(1<n<1e18    ...

  6. 桂林电子科技大学第三届ACM程序设计竞赛 G 路径

    链接:https://ac.nowcoder.com/acm/contest/558/G来源:牛客网 小猫在研究树. 小猫在研究路径. 给定一棵N个点的树,每条边有边权,请你求出最长的一条路径,满足经 ...

  7. C#中异步使用及回调

    1. 一句话理解异步 我叫你去吃饭,叫完你不去,那我就会一直等你,直到你和我一起去吃饭.这叫同步! 我叫你去吃饭,叫完不管你去不去,我都不会等你,我自己去吃饭.这叫异步! 2. 异步使用 static ...

  8. python实现文件的复制

      # 练习: # 1. 写程序,实现文件的复制,(注:只复制文件,不复制文件夹) # 要求: # 1) 要考虑文件关闭的问题 # 2) 要考虑超大文件无法一下加载到内存的问题 # 3) 要能复制二进 ...

  9. Linux三个网络监视器之《二》——nethogs

    当你想要快速了解谁占用了你的带宽时,Nethogs 是快速和容易的.以 root 身份运行,并指定要监听的接口.它显示了空闲的应用程序和进程号,以便如果你愿意的话,你可以杀死它. 1 1.在这个网址f ...

  10. web移动端类型检测

    移动端检测 插件通用下载: https://www.bootcdn.cn/ 根据一个库 device.js 下载地址 传送 api 传送 和 传送 常用检测类型 device.ipad() 返回一个布 ...