Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 10622 Accepted: 3434
DescriptionDearboy 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 Output10
SourcePOJ Monthly--2007.04.01, dearboy
题解 :一对情侣狗洗衣服,两个人可以同时洗不同的衣服,但是必须是同一种颜色不然会染色。
每次有多个输入样例。求洗完衣服的最短用时
1: M N 颜色的种类数/衣服的总数。
2.M行字符串 表示颜色-不超过10个字符不含空格
N行:洗这件衣服的用时 颜色 。
3. 用 0 0 结束。
每件衣服所需总时间就是物品的重量,每件衣服所需的时间即为物品的价值,每种颜色的衣服件数即为物品的数量。
1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <string> 7 #include <cstdlib> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 #include <vector> 12 #include <map> 13 #include <list> 14 //#include <> 15 using namespace std; 16 struct cloth{ 17 char colour[11]; 18 int num; 19 int sumt; 20 int time[105]; 21 }c[11]; 22 int main() 23 { 24 int m,n,i,j,k,t; 25 int dp[500003]; 26 char color[11]; 27 while(scanf("%d%d",&m,&n)&&(m!=0||n!=0)) 28 { 29 for(i=0;i<m;++i) 30 { 31 cin>>c[i].colour; 32 c[i].num=0; 33 c[i].sumt=0; 34 } 35 for(i=0;i<n;++i) 36 { 37 scanf("%d",&t); 38 cin>>color; 39 for(j=0;j<m;++j) 40 { 41 if(strcmp(color,c[j].colour)==0) 42 { 43 int temp=c[j].num; 44 c[j].time[temp]=t; 45 c[j].sumt+=t; 46 c[j].num++; 47 } 48 } 49 } 50 51 int ans=0; 52 for(i=0;i<m;++i) 53 { 54 memset(dp,0,sizeof(dp)); 55 int tempsum=c[i].sumt/2; 56 for(j=0;j<c[i].num;++j) 57 { 58 for(k=tempsum;k>=c[i].time[j];k--) 59 { 60 dp[k]=max(dp[k],dp[k-c[i].time[j]]+c[i].time[j]); 61 } 62 } 63 ans+=c[i].sumt-dp[tempsum]; 64 } 65 cout<<ans<<endl; 66 } 67 return 0; 68 }View Code