POJ3211--分类01背包
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 9700 | Accepted: 3110 |
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 containsM 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
题目大意:
D和他的女朋友两人一块洗衣服,衣服的颜色有很多种,但是每次两人只能洗同一种颜色的衣服。并且洗完这种颜色的衣服才能开始下一种颜色的清洗。问洗完所有衣服的最短时间。
解题思路:
把不同颜色的衣服分别来看。分别求出洗完每种衣服需要的最短时间之后在进行加和就得到了总的最短时间。每种颜色衣服的最短时间就可以看做是不同的01背包,题目给出了洗每件对应的时间,两个人同时进行,极限就是折半。背包的容量就是总时间的一半,得到了一个时间。再用洗完这种颜色衣服的总时间减去这个时间就是最短时间了,因为是“短板效应”,两个人在最快的方案下,最快的时间取决于用时稍长的。最后加和就可以了。代码需要注意的地方就是,下标,因为使用的是结构体里加数组。
源代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; struct node
{
string s;
int flag;//标记这种颜色的衣服有多少件
int p[101];//每件衣服要花费的时间
int sum;//洗这种颜色的衣服一共需要花多少时间
}ans[11];
int dp[100*1000]; void init()//初始化
{
int i;
for(i = 0; i < 10; i++)
{
ans[i].s = "";
memset(ans[i].p,0,sizeof(ans[i].p));
ans[i].flag = 0;
ans[i].sum = 0;
}
} int slove(int k)//每一种颜色进行一次01背包
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 0; i < ans[k].flag; i++)
{
for(j = ans[k].sum/2; j >= ans[k].p[i]; j--)
{
dp[j] = max(dp[j],dp[j-ans[k].p[i]]+ans[k].p[i]);
}
}
return ans[k].sum-dp[ans[k].sum/2];//注意返回值
}
int main()
{
int M,N;
int i,j;
string sa;
int num;
int result;
while(scanf("%d%d",&M,&N)!=EOF&&(M+N))
{
init();//每一次都重新初始化
result = 0;
for(i = 0; i < M; i++)
{
cin>>ans[i].s;
}
for(i = 0; i < N; i++)
{
cin>>num>>sa;
for(j = 0; j < M; j++)
{
if(sa.compare(ans[j].s)==0)
{
ans[j].p[ans[j].flag] = num;
ans[j].sum +=num;
ans[j].flag++;
}
}
}
//分类完成
for(i = 0; i < M; i++)
{
result += slove(i);
}
printf("%d\n",result);
}
return 0;
}
POJ3211--分类01背包的更多相关文章
- POJ3211(trie+01背包)
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9384 Accepted: 2997 ...
- NYOJ-289 苹果 289 AC(01背包) 分类: NYOJ 2014-01-01 21:30 178人阅读 评论(0) 收藏
#include<stdio.h> #include<string.h> #define max(x,y) x>y?x:y struct apple { int c; i ...
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- hdu 2955 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...
- 洛谷P5289 [十二省联考2019]皮配(01背包)
啊啊啊边界判错了搞死我了QAQ 这题是一个想起来很休闲写起来很恶心的背包 对于\(k=0\)的情况,可以发现选阵营和选派系是独立的,对选城市选阵营和学校选派系分别跑一遍01背包就行了 对于\(k> ...
- 2018.09.22 ZJOI2005午餐(贪心+01背包)
描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口)不同,所以他们要吃的菜各有不 ...
- Codeforces 336C 0-1背包
题意:每个水果有两个值,一个美味度 a,一个卡路里 b,从中挑选一些,要求 sum(aj) / sum(bj) = k,使得 sum(a) 最大. 分析:没有那个条件就是一个01背包,可以转换,对公式 ...
- NYOJ(325)+NYOJ(456),01背包
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=325 http://acm.nyist.net/JudgeOnline/problem. ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
随机推荐
- 异步API
整合到一起,作为参考: //2 5 3 4 1 //2 5 3 1 4 var img = new Image(); img.src = '.'; img.onerror = function() { ...
- Java中的类变量、实例变量、类方法、实例方法的区别
类变量:形如static int a; 顾名思义,类变量可以理解为类的变量,类变量在类加载的时候就已经给它分配了内存空间,不同于实例变量(int a; ),实例变量是在该类创建对象的时候分配内存的.并 ...
- poj 1759 Garland
Garland Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2365 Accepted: 1007 Descripti ...
- sersync实现数据实时同步
1.1 第一个里程碑:安装sersync软件 1.1.1 将软件上传到服务器当中并解压 1.上传软件到服务器上 rz -E 为了便于管理上传位置统一设置为 /server/tools 中 2.解压软件 ...
- appendChild方法详解
方法:target.appendChild(ele); 执行该方法时,会发生两部操作: 1.将元素ele从原来的父元素中移除掉 2.将元素追加至新的目标元素中,并且保留元素的所有样式信息和事件... ...
- 开源API测试工具 Hitchhiker v0.4更新 - 没有做不到,只有想不到
Hitchhiker 是一款开源的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍 ...
- 使用java生成mapbox-gl可读的vector tile
概述 mapbox-gl主要数据源来自mapbox vector tile,本文就是要阐述怎样把postgresql中的地理空间数据转换成vector tile,流程图如下: 配置 该工程采用spri ...
- java 通过eclipse编辑器用mysql尝试 连接数据库
注:本人学的是Oracle,用mysql连接数据库是一次尝试. 一.下载JDBC mysql驱动,导入jar包 我自己下载的是connector-java-6.0.6.jar,如下图所示,JD ...
- 非对称加密技术- RSA算法数学原理分析
非对称加密技术,在现在网络中,有非常广泛应用.加密技术更是数字货币的基础. 所谓非对称,就是指该算法需要一对密钥,使用其中一个(公钥)加密,则需要用另一个(私钥)才能解密. 但是对于其原理大部分同学应 ...
- 使用wwise音效引擎的好处
用过一段时间的wwise,做以下几个具体功能的时候比较方便: 1.当策划需求一个声音需要随机播放多个随机音源的其中一个时,例如脚步声.普通攻击声,当这类声音一直播放的都是同一个音源的时候,人会产生听觉 ...