01背包的变形
2014-12-18 10:15
155 查看
http://poj.org/problem?id=3211
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
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
/** poj3211 题目大意:夫妻两个人一起洗衣服,洗衣速度一样快,衣服有多种颜色,要求只有在完全洗完一种颜色的衣服后才能洗另一种颜色的,问所有的衣服都洗完最少用时是多少? 解题思路:把相同颜色的衣服看做一类,统计总时间sum然后将sum尽可能的均分的分给两个人(借助01背包,详见代码),取最大值加入ans。最后所有一份都统计完,ans即为答案。 */ #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; struct node { int ip; char a[25]; }p[105]; bool cmp(node x,node y) { return strcmp(x.a,y.a)>0; } int n,m; int s[1005],dp[100005]; int main() { while(~scanf("%d%d%*c",&m,&n)) { if(n==0&&m==0) break; char b[1005]; gets(b); for(int i=0;i<n;i++) scanf("%d%s",&p[i].ip,p[i].a); p .ip=0; strcpy(p[n++].a,"####"); sort(p,p+n,cmp); /* for(int i=0;i<n;i++) printf("%d %s\n",p[i].ip,p[i].a);*/ int sum=0,k=1,ans=0; for(int i=0;i<n-1;i++) { if(strcmp(p[i].a,p[i+1].a)==0) { s[k++]=p[i].ip; sum+=s[k-1]; } else { s[k]=p[i].ip; sum+=s[k]; memset(dp,0,sizeof(dp)); int cost=sum/2; for(int j=1;j<=k;j++) { for(int l=cost;l>=s[j];l--) dp[l]=max(dp[l],dp[l-s[j]]+s[j]); } ans+=max(dp[cost],sum-dp[cost]); sum=0; k=1; } } printf("%d\n",ans); } return 0; } /** 3 1 red blue yellow 2 red */
相关文章推荐
- HUD 1881 毕业bg(01背包变形)
- hdu 2955 01 背包小变形
- BNU 11993 Soccer Teams (01背包变形+数位dp)
- POJ 2184 Cow Exhibition 奶牛展(01背包,变形)
- Hdu 3236 Gift Hunting(01背包变形)
- hdu1171(01背包变形)
- HDU 3033 I love sneakers!(01背包变形)
- POJ 1837 Balance(01背包变形, 枚举DP)
- poj3211-01背包变形
- POJ 2184 Cow Exhibition(01背包变形)
- hdu1171 Big Event in HDU (01背包变形)
- 九度 1499:项目安排(任务调度, 01背包变形)
- HDU 2955 Robberies抢劫案(01背包,变形)
- poj2184(01背包变形)
- soj 2142: Cow Exhibition(01背包的变形)
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
- 51nod动态规化入门--01背包变形
- (POJ1837)Balance <01背包变形经典题>
- UVA 12563 Jin Ge Jin Qu hao 01背包变形
- uva 12563 Jin Ge Jin Qu hao (01背包变形)