poj3211Washing Clothes(分成多组01背包) .......经典
来源:互联网 发布:澳洲工资算法 编辑:程序博客网 时间:2024/06/05 14:07
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 4red blue yellow2 red3 blue4 blue6 red0 0
Sample Output
10
题目意思:小两口洗衣,两人洗完同一种颜色的衣服,两人才能洗下一种颜色的衣服,问洗完全部的衣服须要最少的时间。
解题:由于洗完同一种颜色是两人同时洗,那么要洗完同一种的衣服最少要花总时间的一半,那么我们可以用总时间的一半来作一个背包求出一个人洗衣在一半的时间里用的最多的时间t,那么 总时间-t=两人用时最少洗完这同一颜色的衣服。
#include<stdio.h>#include<string.h>struct node{ int k,sum;//这种颜色的件数和总时间 int time[105];//每件衣的用时}c[15];//同一类的衣服int main(){ int n,m,tim,dp[100005]; char color[15][50],col[50]; while(scanf("%d%d",&n,&m)>0&&n+m!=0) { for(int i=1;i<=n;i++) { scanf("%s",color[i]); c[i].k=0; c[i].sum=0; } for(int i=1;i<=m;i++) { scanf("%d%s",&tim,col); for(int j=1;j<=n;j++) if(strcmp(col,color[j])==0) { c[j].k++; c[j].sum+=tim; c[j].time[c[j].k]=tim; } } tim=0; for(int i=1;i<=n;i++) if(c[i].k) { memset(dp,0,sizeof(dp)); int hlf=c[i].sum/2; for(int e=1;e<=c[i].k;e++)//第i种颜色的衣服第e件 { int use=c[i].time[e]; for(int u=hlf;u>=use;u--)//01背包 if(dp[u]<dp[u-use]+use) dp[u]=dp[u-use]+use; } tim+=(c[i].sum-dp[hlf]); } printf("%d\n",tim); }}
- poj3211Washing Clothes(分成多组01背包) .......经典
- POJ3211Washing Clothes(分组01背包)
- POJ3211Washing Clothes
- poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)
- POJ3211Washing Clothes题解动态规划DP
- POJ 3211 Washing Clothes【01背包】
- poj 3211 Washing Clothes(01背包问题)
- POJ 3211 Washing Clothes【01背包+STL】
- poj 3211 Washing Clothes(01背包)
- poj 3211 Washing Clothes 01背包
- poj3211 Washing Clothes(多次01背包)
- poj 3211 Washing Clothes 01背包
- poj 3211 Washing Clothes 01背包问题
- poj 3211 Washing Clothes 分组01背包
- POJ 3211Washing Clothes(01背包)
- Washing Clothes - POJ 3211 01背包
- POJ 3211 Washing Clothes(01背包)
- POJ 3211 - Washing Clothes(01背包)
- CMake实践
- 第一周项目三某天是该年的第几天
- 误删除日志文件导致出现 ORA-01034&ORA-27101错误
- android utest 测试
- [Windows/MacOSX]_初级_[在英文环境下不使用软键盘输入希腊字母]
- poj3211Washing Clothes(分成多组01背包) .......经典
- C3P0简单配置
- 链接sqlserver2008错误提示及解决办法
- C#记事本查找与替换问题??
- Sql Server 2008 配置管理器 无法连接WMI提供程序
- 学生成绩统计
- CreateIoCompletionPort和完成端口
- 不使用第三个变量实现两个整数之间的交换
- 升级版。。