Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9213 | Accepted: 2946 |
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 and N (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 4 red blue yellow 2 red 3 blue 4 blue 6 red 0 0
Sample Output
10
题目大意:一个男的和他女盆友一起洗衣服,衣服的颜色最多分为十类,为了让这些颜色不同的衣服不相互染色,得把颜色不同的衣服分开洗,给出每一件衣服的颜色以及所花的时间,求他们最少要花多少时间才能把所有衣服都洗完?
因为两个人同一时间内只能洗相同颜色的衣服,所以可以把这些衣服分类,分别求出洗完各类颜色衣服所花的最少时间,求和即为结果,
对于每一种颜色的衣服,都可以用0-1背包求其所花的最少时间,【上文中所说的最短时间,最理想的状态为两人洗的衣服所花的时间相等,无论如何都不会想等,就尽量让两人的时间差最小】。【用0-1背包,可以把洗衣服所花的时间视为价值和体积】。
struct Clo{
int color; //衣服颜色;
int num; //该颜色的衣服总共有多少件;
int sum; //一个人要完这种颜色的衣服所花的时间;
int time[101]; //洗每件衣服所花的时间;
}clo[12]; //因为衣服的颜色最多只有10种。
已Accept代码【c++提交】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
int dp[100005];
char str[11][12];
struct Clo {
int time[101];
int num;
int color;
int sum;
}clo[11];
void solve() {
int total = 0;
for(int i = 0; i < n; i++) {
memset(dp, 0, sizeof(dp));
int sum = clo[i].sum / 2;
for(int j = 0; j < clo[i].num; j++) {
for(int k = sum; k >= clo[i].time[j]; k--) {
dp[k] = max(dp[k], dp[k - clo[i].time[j]] + clo[i].time[j]);
}
}
total += clo[i].sum - dp[sum];
}
printf("%d\n", total);
}
int main() {
char buf[15];
int time;
while(scanf("%d%d", &n, &m), n | m) {
for(int i = 0; i < n; i++) {
scanf("%s", str[i]);
clo[i].num = clo[i].sum = 0;
clo[i].color = i;
}
for(int i = 0; i < m; i++) {
scanf("%d %s", &time, buf);
for(int j = 0; j < n; j++)
if(!strcmp(str[j], buf)) {
int tmp = clo[j].num;
clo[j].time[tmp] = time;
clo[j].sum += time;
clo[j].num++;
break;
}
}
solve();
}
return 0;
}