Q: 01背包最后返回什么 dp[v], v 是多少?

A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可

update 2014年3月14日11:22:55

1. 几个月后, 感觉返回的不应该是 dp[V], 二是 dp[0...V] 中的最大值

Description

Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes are in varieties of colors but each piece of them can be seen as of only one color. In order to prevent the clothes from getting dyed in mixed colors, Dearboy and his girlfriend have to finish washing all clothes of one color before going on to those of another color.

From experience Dearboy knows how long each piece of clothes takes one person to wash. Each piece will be washed by either Dearboy or his girlfriend but not both of them. The couple can wash two pieces simultaneously. What is the shortest possible time they need to finish the job?

Input

The input contains several test cases. Each test case begins with a line of two positive integers M and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line contains M strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then follow N lines describing the clothes. Each of these lines contains the time to wash some piece of the clothes (less than 1,000) and its color. Two zeroes follow the last test case.

Output

For each test case output on a separate line the time the couple needs for washing.

Sample Input

3 4
red blue yellow
2 red
3 blue
4 blue
6 red
0 0

Sample Output

10

思路:

1. 先将颜色相同的衣服聚类到一起

2. 对某一种颜色的衣服两人开洗, 这就涉及到01背包的变形, 即如何分配衣服使得总时间最小. 解法是将背包容量设置为总容量的一半, 然后进行01背包

总结:

1. map 和 vector<vector<> > 搭配的不够默契, 与 vector in[MAXN] 比较默契

2. memset 减少时间的方法, memset(dp, 0, sizeof(int)*(V+1));

3. 这里, 因此 V 是 sum/2 后的结果, 所以最终会填满背包, 返回 dp[V] 即可

代码:

#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std; int M, N;
vector<int> cloth[11];
map<string, int> color;
int dp[100010];
int solve_dp() {
int sum = 0;
for(int index = 0; index < M; index++) {
int V = 0;
for(int j = 0; j < cloth[index].size(); j ++) {
V += cloth[index][j];
}
int rem = V&1;
memset(dp, 0, sizeof(int)*(V+1));
V = V>>1;
for(int j = 0; j < cloth[index].size(); j ++) {
int wj = cloth[index][j];
for(int k = V; k >= wj; k --) {
dp[k] = max(dp[k], dp[k-wj]+wj);
}
}
sum += ((V<<1)+rem)-dp[V]; }
return sum;
} int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin); while(scanf("%d%d", &M, &N) && M != 0) {
string str;
color.clear();
for(int i = 0; i < M; i ++)
cloth[i].clear();
for(int i = 0; i < M; i ++) {
cin >> str;
color[str] = i;
}
int t;
for(int i = 0; i < N; i ++) {
cin >> t >> str;
cloth[color[str]].push_back(t);
}
cout << solve_dp() << endl;
}
return 0;
}

  

POJ 3211 Washing Cloths(01背包变形)的更多相关文章

  1. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  2. POJ 3211 Washing Clothes 0-1背包

    题目大意: xxx很懒,但他有个漂亮又勤奋的女友 (尼玛能不能不刺激我,刚看到这题的时候发现自己的衣服没洗!!!) 可以帮他洗衣服. 洗衣服的时候要求不同的颜色的衣服不能同时洗.一人洗一件的话,问最短 ...

  3. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  5. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  6. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  7. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  9. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

随机推荐

  1. electron 的跳转

    // 测试 正常跳转应该登录成功 // that.timer = setInterval(() => { that.$router.push('/mainChat');//路由跳转mainCha ...

  2. arm B和BL指令浅析

    arm B和BL指令浅析 B或BL指令引起处理器转移到“子程序名”处开始执行.两者的不同之处在于:(1)BL指令在转移到子程序执行之前,将其下一条指令的地址拷贝到R14(LR,链接寄存器).      ...

  3. sqlserver 关于子查询的优化操作

    在对条件需要用到子查询的时候,通常可以通过连接来代替子查询来完成操作,更加高效 SELECT t1.id,t2.columnName,t2.columnType,t1.rownum,t1.cellVa ...

  4. TensorFlow机器学习框架-学习笔记-001

    # TensorFlow机器学习框架-学习笔记-001 ### 测试TensorFlow环境是否安装完成-----------------------------```import tensorflo ...

  5. 【Unity笔记】常用插件

    记录一些常见插件,随时补充. iTween动画插件 原理:插值法,给出初始值和终点值,自动算出中间值. DoTween Tween动画 Playmaker $45 Playmaker由第三方软件商Hu ...

  6. 【Unity】使用SceneManager加载/切换场景

    一.直接切换 老版的(已弃用!): Application.LoadLevel(int index); // 参数是场景编号 Application.LoadLevel(string name); / ...

  7. java并发编程()阻塞方法与中断方法

    看完这篇,我感觉我对java多线程又懵逼了. 线程可能会阻塞或暂停执行,原因有多种: 等待I/O操作结束 等待获得一个锁 等待从Thread.sleep方法中醒来 等待另一个线程计算的结果 当线程阻塞 ...

  8. java-jdbc循环设置sql参数

    PreparedStatement sta=null; Connection conn=null; @Test public void Test2() { //获取连接池 ComboPooledDat ...

  9. 看板与Scrum:哪个更适合你的团队?

    敏捷是理想型指标和原则,看板和Scrum是帮助团队坚持敏捷原则并完成工作的基本框架.本文详细介绍了在Scrum和看板之间做出选择时要考虑的关键因素,以及如果我们无法做出决定时该怎么办. Scrum和看 ...

  10. laravel 5.1 的程序性能优化(配置文件)

    命令优化 本文的目的是来弄清楚一些优化命令在 Laravel 5.1 和之前版本之间的差别. 在 15年6月发布的 Laravel 5.1版本中, 命令和他们的逻辑方法被清理掉, 本文章就是描述这些不 ...