POJ3211--分类01背包
来源:互联网 发布:anaconda 改成python 2 编辑:程序博客网 时间:2024/04/27 19:34
Memory Limit: 131072KTotal 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 4red blue yellow2 red3 blue4 blue6 red0 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-01背包变形
- poj3211(01背包)
- poj3211 转换 01背包
- 01背包 poj3211 STL助攻
- poj3211 Washing Clothes(多次01背包)
- poj3211 Washing Clothes(01背包)
- POJ3211:Washing Clothes(分组01背包)
- poj3211(背包dp)
- 01背包入门题解--poj3624、poj3628、hdu1864、poj3211
- poj3211(分组背包。。。大概)
- poj3211
- poj3211
- poj3211(Washing Clothes + 0/1背包)
- Washing Clothes poj3211 0-1背包
- 经典动态规划问题 背包问题 [POJ3211][POJ2063][HDU1114][POJ1276][POJ1014][POJ1742][ZOJ3631]
- POJ3211洗衣服
- hdu 3535 AreYouBusy(多条件分类01背包)
- 插入排序、希尔排序、冒泡排序、快速排序、选择排序、堆排序、归并排序
- css3媒体查询
- gcc configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+
- 使用bootstrap-pagination进行分页
- 2016-8-9夏令营总结(网络流—增广路+dinic)
- POJ3211--分类01背包
- 32位单片机 一个32位地址代表一个字节而不是4个字节(32位)
- HTML5-CSS布局
- javascript 面向对象
- 配置standby redo log
- my学习OC--类的构造函数和析构函数
- PHP复用curl请求遇到的请求参数混乱的问题
- 【NOIP2016提高A组模拟8.14】传送带 (三分套三分)
- 最受欢迎的十个开源大数据技术