Washing Clothes (01背包题一)

来源:互联网 发布:java se环境包括jdk吗 编辑:程序博客网 时间:2024/06/05 04:44
Washing Clothes
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 9001 Accepted: 2859

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 integersM andN (M < 10, N < 100), which are the numbers of colors and of clothes. The next line containsM strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then followN 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
这一题是在这里看到的:http://blog.csdn.net/shuangde800/article/details/8953243

这一题的思路呢大体和(之前的邮票分你一半)很相似,是那一题的加强版,因为这一题需要把属于同一种颜色的衣服的最小时间算出来,然后相加求和。 

// 时刻不要忘记dp的意义。 #include<iostream>#include<queue>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<string>#define MP make_pair -------------#define SQ(x) ((x)*(x))   //宏定义sq函数,返回参数x的平方的值 using namespace std;typedef long long int64; const int MAXN = 110;const int INF = 0x3f3f3f3f;int n, m;int h[25];int w[12][MAXN], sum[12], num[12];  int f[MAXN*1000];              int main(){    char color[12];    int t;    while(~scanf("%d%d", &n, &m) && n+m){   //n代表颜色的数量,m代表衣服的个数         map<string, int>mp;        for(int i=0; i<n; ++i){  // 输入颜色             scanf("%s",color);            mp[color] = i;  //给颜色排上编号         }        for(int i=0; i<=n; ++i)             sum[i] = num[i] = 0;        for(int i=0; i<m; ++i){            scanf("%d%s", &t, color);    //输入衣服洗得时间和颜色             int pt=mp[color];     //获得颜色的编号             w[pt][num[pt]++] = t;     //该编号的颜色,该颜色衣服中的第几件,洗这件衣服需要的时间             sum[pt] += t;           // 该种颜色的衣服洗完一共需要多少时间         }        int ans = 0;     //答案值 answer。。。。。初始化         for(int k=0; k<n; ++k)                    //按照颜色来,一种一种的算时间; {            for(int i=0; i<=sum[k]/2; ++i) f[i] = 0;                        for(int i=0; i<num[k]; ++i)    // i 表示从0开始,到第k种颜色的最后一个的遍历 {                for(int v=(sum[k]>>1); v>=w[k][i]; --v)  // sum[k] >>1 的意思是sum[k]除以二 ,v表示总时间的迭代                     f[v] = max(f[v], f[v-w[k][i]]+w[k][i]);            }            ans += sum[k]-f[sum[k]>>1];   // 加上第k种颜色的衣服所需的最大时间。         }        printf("%d\n",ans);   // 输出总的最小时间         cout<<INF<<endl;        cout<<INF*2<<endl;        cout<<0x7fffffff<<endl;    }    return 0;}

人家的程序,我只是读懂了。。。


0 0
原创粉丝点击