POJ3211 Washing Clothes[DP 分解 01背包可行性]
2016-08-24 10:32
253 查看
Washing Clothes
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
Sample Output
Source
POJ Monthly--2007.04.01, dearboy
-------------------------------------------------------
每种颜色相互独立,单独求解加起来即可(好像矩阵取数)
对于一种颜色,平均分配,假设时间和为sum,其中一个人的时间就是给一个容量为sum/2的背包装东西,v和w都是时间(01背包可行性,不一定装满,所以要保存w而不是1)
这样背包的价值就是一个人的用时,另一个人的用时是sum-背包价值,更新ans即可
[注意:和搭建双塔不同,本题一定要用所有“物品”,所以不用那么麻烦]
Bonus:每件衣服两人花的时间不同?
f[i] 第一个人用时i时第二个人的最短用时
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9707 | Accepted: 3114 |
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
Source
POJ Monthly--2007.04.01, dearboy
-------------------------------------------------------
每种颜色相互独立,单独求解加起来即可(好像矩阵取数)
对于一种颜色,平均分配,假设时间和为sum,其中一个人的时间就是给一个容量为sum/2的背包装东西,v和w都是时间(01背包可行性,不一定装满,所以要保存w而不是1)
这样背包的价值就是一个人的用时,另一个人的用时是sum-背包价值,更新ans即可
[注意:和搭建双塔不同,本题一定要用所有“物品”,所以不用那么麻烦]
Bonus:每件衣服两人花的时间不同?
f[i] 第一个人用时i时第二个人的最短用时
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <string> using namespace std; const int N=105,M=15,T=1e5+5; int m,n,num=0,cnt[M],ans=0; string s; map<string,int> mp; struct clo{ int t,col; }a ; int f[T]; void dp(){ for(int c=1;c<=m;c++){ int sum=cnt[c]; memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) if(a[i].col==c) for(int j=sum/2;j>=a[i].t;j--) f[j]=max(f[j],f[j-a[i].t]+a[i].t); ans+=cnt[c]-f[sum/2]; } } void init(){ mp.clear(); num=ans=0; memset(cnt,0,sizeof(cnt)); } int main(){ while(true){ scanf("%d%d",&m,&n); if(m==0&&n==0) break; init(); for(int i=1;i<=m;i++){ cin>>s; mp[s]=++num; } for(int i=1;i<=n;i++){ scanf("%d",&a[i].t); cin>>s; a[i].col=mp[s]; cnt[a[i].col]+=a[i].t; } dp(); printf("%d\n",ans); } }
相关文章推荐
- 简单01背包 POJ3211 Washing Clothes 多种衣服分别dp
- openjudge2989糖果[DP 01背包可行性]
- HDU 2546饭卡(DP:01背包)
- hdu 2602 DP 01背包
- HDU 1561 The more, The Better 【树形dp + 依赖性01背包 】这类型入门题.
- hdu——2955(dp之01背包,但需要转化下思维)
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
- DP专题->01背包
- DP实例之01背包问题C语言实现
- HDU 2955 Robberies(DP:01背包)
- newoj 21496: 开心的金明【01背包dp】
- hdu1561树形dp 01背包
- HDU 3466 Proud Merchants(DP:01背包+贪心)
- HDU 2639 Bone Collector II(DP:01背包)
- POJ 3624 【DP入门之01背包】
- 夕拾算法进阶篇:19)采药 (01背包_动态规划DP)
- poj3211-01背包变形
- HDU5543 Pick The Sticks(01背包变种,dp)(2015CCPC)
- 【DP学习】之 01背包
- 061_改变DP对象的01背包问题