POJ 3211 (分组01背包) Washing Clothes

题意:

小明有一个贤妻良母型的女朋友,他们两个一起洗衣服。

有M种颜色的N件衣服,要求洗完一种颜色的衣服才能洗另外一种颜色。

两人可以同时洗,一件衣服只能被一个人洗。

给出洗每件衣服所用的时间,求两个人洗完这些衣服所用的最短时间。

分析:

因为每种颜色是分开洗的,所以我们可以单独考虑一种颜色的衣服。

因为洗完这些衣服的总时间是固定的,所以两个人洗的时间尽可能的相等,这样洗完的时间最短。

所以将总时间的一半作为背包容量(这里总时间的奇偶性并不影响),物品的体积和价值都是洗每件衣服所用的时间,然后进行01背包。

所求答案就是总时间减去背包的最大价值。

 1 #include <iostream>
 2 #include <map>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <string>
 7 #include <cstring>
 8 using namespace std;
 9
10 int dp[50000 + 50];
11
12 int main()
13 {
14     freopen("in.txt", "r", stdin);
15     int M, N;
16     while(scanf("%d%d", &M, &N) == 2 && M && N)
17     {
18         map<string, vector<int> > cloth;
19         string s;
20         int l, ans = 0 ;
21         vector<string> str;
22         getchar();
23         for(int i = 0; i < M; ++i)
24         {
25             cin >> s;
26             str.push_back(s);
27         }
28         for(int i = 0; i < N; ++i)
29         {
30             cin >> l >> s;
31             cloth[s].push_back(l);
32         }
33         for(int i = 0; i < M; ++i)
34         {
35             if(cloth[str[i]].empty()) continue;
36             int n = cloth[str[i]].size();
37             int sum = 0;
38             for(int j = 0; j < n; ++j)
39                 sum += cloth[str[i]][j];
40
41             memset(dp, 0, sizeof(dp));
42             int V = sum / 2;
43             for(int j = 0; j < n; ++j)
44                 for(int k = V; k >= cloth[str[i]][j]; --k)
45                     dp[k] = max(dp[k], dp[k-cloth[str[i]][j]] + cloth[str[i]][j]);
46
47
48             ans += (sum - dp[V]);
49         }
50
51         printf("%d\n", ans);
52     }
53
54     return 0;
55 }

代码君

时间: 2024-08-28 05:57:53

POJ 3211 (分组01背包) Washing Clothes的相关文章

POJ 2184--Cow Exhibition(0-1背包变形)

题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by

POJ 2923 【01背包+状态压缩/状压DP】

题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit.

POJ 2184(01背包)(负体积)

http://poj.org/problem?id=2184 http://blog.csdn.net/liuqiyao_01/article/details/8753686 对于负体积问题,可以先定义一个“零点”shift,将dp[shift]设为0,其他都设为-INF. 然后负体积从0往maxn+cost更新,正体积从maxn往shift更新. 最后从shift到maxn中找最大值,shift以下的值不会被遍历到 #include <iostream> #include <strin

poj 1717 Dominoes 01背包

题意: 给两列数,a1,a2..an和b1,b2..bn,可以交换ak和bk,求让两列数和的差的绝对值最小的最少交换次数. 分析: 动态规划,dp[x]表示a1,..am进过交换达到和为x的最小交换次数.dp[x+a]<-dp[x],dp[x+b]<-dp[x]+1. 代码: //poj 1717 //sep9 #include <iostream> using namespace std; const int maxM=12000; int dp[maxM+10]; int a,

poj 3624 (01背包一维数组)

①这种问题要看清楚变量范围 数组范围很容易开错!!! ②这个下限是w[i]!!!很重要 或者加一句判断  if(j-w[i]>=0)  1 for(int i=1;i<=t;i++) 2 { 3 for(int j=n;j>=w[i];j--)//下限!!! 4 dp[j]=max( dp[j] , dp[j-w[i]]+v[i] ); 5 } AC代码,一维数组可以压缩空间. 1 #include <cstdio> 2 #include <cstring> 3

POJ 3624 (01背包)

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'd

POJ 1837 Balance (01背包)

题意:有一个天平,左臂右臂各长15,然后给出n,m.n代表有几个挂钩,挂钩给出负数代表在左臂的距离,正数则在右臂:m代表有m个砝码,要你求出使得这个天平保持平衡有几种方法,要求所有砝码全部使用完. 思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为放置的砝码数量,j为平衡状态,0为平衡,j<0左倾,j>0右倾,由于j作为下标不能是负数,所以我们要找一个新的平衡点,因为15*20*20 = 7500,所以平衡点设置为7500,然后我们可以得出动态方程 dp[i][j+w[i]*c[k

poj 3211 Washing Clothes 0-1背包

题意: 有2个人洗n件衣服,每件衣服有需要洗的时间和颜色,只能相同颜色的衣服两人一起洗,求洗完衣服的最少时间. 分析: 0-1背包判断某个重量是否能达到. 代码: //poj 3211 //sep9 #include <iostream> #include <map> #include <string> using namespace std; const int maxN=128; int m,n; map<string,int> name; int v[

POJ 3211 Washing Clothes(01背包)

http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件,Dearboy和她的girlfriend两个人要一起洗完全部衣服,为了预防色彩混合,他们每次只能同时洗同一种颜色的衣服,给出洗完每件衣服所需的时间time和它的颜色color,求出Dearboy和她的girlfriend最少用多少时间能洗完成全部衣服. 分析: 由于每种颜色的衣服是分开洗的, 所以我们可以把所有衣服按颜色分类, 然后每次看洗一种颜色的衣服最少需要花多少