Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7063 | Accepted: 2089 |
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
Source
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include
13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 using namespace std;20 21 int n,m; //m表示衣服的颜色种类,n表示衣服的件数 22 int dp[10001]; //1000*10+1 23 24 struct clothes //某一种颜色的结构体 25 {26 int num[1001],cnt,sum; //num表示洗每一件这种颜色的衣服的时间,cnt表示有几件衣服,sum表示总时间 27 char type[26]; // type表示这种衣服的颜色 28 }clo[26];29 30 int main()31 {32 int i,j,k;33 char temp[26];34 int num;35 int min;36 int ans;37 while(~scanf("%d%d",&m,&n),(m||n))38 {39 memset(clo,0,sizeof(clo));40 for(i=0;i =clo[i].num[j];k--) //01背包从最大空间开始递减 62 {63 if(dp[k-clo[i].num[j]]+clo[i].num[j]>dp[k])64 dp[k]=dp[k-clo[i].num[j]]+clo[i].num[j];65 }66 }67 ans+=clo[i].sum-dp[clo[i].sum/2];68 }69 //-------------------------------DP-------------------------------------------------------------------------- 70 printf("%d\n",ans);71 }72 return 0;73 } 74 75 76 //注意数组的大小,WA了一次