[Tom and Bag][需要记录过程的dp]
http://acm.beihua.edu.cn/problem/1007
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]的更多相关文章
- ImportError: No module named tornado.ioloop 记录过程
ImportError: No module named tornado.ioloop 记录过程 安装 pycurl pip install pycurl 报错 'curl-config' no ...
- 【Oracle RAC】Linux系统Oracle18c RAC安装配置详细记录过程(图文并茂)
本文Oracle 18c GI/RAC on Oracle Linux step-by-step 的安装配置步骤,同时也包含dbca 创建数据库的过程. 1. 关闭SELINUX,防火墙vi /etc ...
- 【学习笔记&训练记录】数位DP
数位DP,即对数位进行拆分,利用数位来转移的一种DP,一般采用记忆化搜索,或者是先预处理再进行转移 一个比较大略的思想就是可以对于给定的大数,进行按数位进行固定来转移记录答案 区间类型的,可以考虑前缀 ...
- code forces 148D Bag of mice (概率DP)
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 经测试稳定可用的蓝牙链接通信Demo,记录过程中遇到的问题的思考和解决办法,并整理后给出一个Utils类可以简单调用来实现蓝牙功能
说明:这是本人在蓝牙开发过程中遇到过的问题记录和分析,以及解决办法. 在研究过程中,许多的前人给出的解决方案和思路指导对我相当有帮助,但并非都是可采取的解决方法, 经过本人对这些方法的测试和使用过后, ...
- (最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160
http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Other ...
- 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 ...
- CF 148D Bag of mice【概率DP】
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes Promblem descriptio ...
- CF148D. Bag of mice(概率DP)
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- Mysql 了解changeBuffer 与 purge 调优
需要删除.新增记录或更新一个数据页时,如果数据页在内存中就直接更新,而如果这个数据页还没有在内存中的话,在不影响数据一致性的前提下,InooDB 会将这些更新操作缓存在 change buffer中, ...
- nginx常用模块
Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...
- Altium Designer 使用中的小技巧1
在布线的过程中所学到的一点技巧:在没有画原理图的情况下,直接绘制PCB板,需要敷铜Ppolygon pour,但没有网络标号,就无法连上要连的网络,焊盘,怎么办呢?需要事先将需要连接在一起的元器件(焊 ...
- asp.net webAPI POST方法的CORS跨域问题
端口不同会判断为不同域 Method Not Allowed . web.config中设定·customHeaders 错误变化为 原因‘ post方法使用前会有一次OPTION方法的请求’ 解决: ...
- powershell脚本:你的文件已经被黑客篡改.ps1
本人原创powershell脚本分享. 脚本用途:列出某目录下,所有软件签名不符的文件. 系统需求: win7 + powershell 2.0 及 以上. #nd你的文件已经被黑客篡改.ps1 ps ...
- JAVA 第1课
JAVA第一课 电脑识别的进制:二进制,八进制,十六进制 二进制来表示高低电压,类似于抗战时期的发报机.2进制的存储 8进制和16进制:计算器,在计算的时候有一定的临时存储,8位或者16位禁止的存储 ...
- jar包添加到maven本地仓库
操作系统windows 本地要配置过maven环境 cmd 运行命令 mvn install:install-file -Dfile=D:\commons-net-3.6.jar.jar -Dgro ...
- 第2次作业 -- 熟悉 JUnit 测试
2.1 Mooctest 使用心得 Mooctest很方便,可以即时测评自己写的测试代码,获得覆盖率和报告,不需要自己安装配置环境 而且安装配置插件的环境也很简单,可以专注于测试本身 2.2 Juni ...
- Centos7搭建docker仓库
一:安装启动registry 1.1:环境准备 yum install -y python-devel libevent-devel python-pip gcc xz-devel pip insta ...
- 基于centos6.5安装部署mongdb3.6
注意:不同的版本的centos,mongdb安装方式不同,请注意版本号!! 基于centos6.5安装部署mongdb3.6 方式有多种,本文介绍使用wget命令来下载获取mongdb,具体命令如下 ...