Description
Dearboy was, so busy recently, he had piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes is in varieties of colors but each piece of them can is seen as of only one color. In order to prevent the clothes from getting dyed on mixed colors, dearboy and his girlfriend has to finish washing all C Lothes of one color before going on to those of another color.
From experience Dearboy knows how long is piece of clothes takes one person to wash. Each piece is washed by either Dearboy or his girlfriend and not both of them. The couple can wash the pieces simultaneously. What's 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 positive integers m and n (m < n < Which is the numbers of colors and of clothes. The next line contains M strings which is not longer than ten characters and do not contain spaces, which the NAM Es 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 $) and its color. Zeroes follow the last test case.
Output
For each test, the output on a separate line the time of the couple needs for washing.
Sample Input
3 4red Blue yellow2 red3 blue4 blue6 red0 0
Sample Output
10
This problem is the deformation of the group backpack, first use map to each string corresponding to each number, and then the same string into a class, to find the minimum time, and then add up on the line, here each group of the shortest time can be used 01 backpack, because it is two people do at the same time, you can know the difference between the workload of the least It takes the least amount of time to end a dress of this color, so you can set the backpack capacity to SUM[I]/2 and then ask for the maximum capacity of the volume, and then the time that this group adds is SUM[I]-DP[SUM[I]/2]. It's also important to note that initialization cannot be initialized to 1, and then the DP [0]=0, because it doesn't have to happen, take a closer look:)
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #include < math.h> #include <vector> #include <map> #include <queue> #include <stack> #include < String> #include <algorithm>using namespace Std;int a[15][106],num[106],sum[106],dp[100500];int main () {int n , M,i,j,t,c,ans,liang,k,num1;char S[20];map<string,int>hash;while (scanf ("%d%d", &n,&m)!=EOF) {if (n== 0 && m==0) break;memset (A,0,sizeof (a)), memset (num,0,sizeof (num)), memset (sum,0,sizeof (sum)), memset (s,0, sizeof (s)); Hash.clear (); Num1=0;for (i=1;i<=n;i++) {scanf ("%s", s); if (hash[s]==0) {num1++;hash[s]=num1;}} for (i=1;i<=m;i++) {scanf ("%d%s", &c,s); t=hash[s];a[t][++num[t]]=c;sum[t]+=c;} Ans=0;for (i=1;i<=num1;i++) {if (num[i]==0) Continue;else{//memset (Dp,-1,sizeof (DP)); memset (Dp,0,sizeof (DP)); Liang=sum[i]/2;for (k=1;k<=num[i];k++) {for (j=liang;j>=a[i][k];j--) {//if (dp[j-a[i][k]]!=-1) Dp[j]=max (Dp[j] , Dp[j-a[i][k]]+a[i][k]);}} Ans=ans+sum[i]-dp[liang];}} printf ("%d\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
poj3211 Washing Clothes