POJ 3211 Washing Clothes(01背包)@
来源:互联网 发布:手机淘宝电脑版在哪里 编辑:程序博客网 时间:2024/05/17 05:19
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
因为衣服用的时间有大有小,大的可以包含小的,小的还可以累积,,所以就开总时间的一半作为背包
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <string>#include <sstream>#include <vector>using namespace std;const int N = 100010;const int inf = 999999999;int dp[N], dp1[N];char str[N], s[N];map<string , int>p;vector<int>G[N];int main(){ int m, n, x; while(scanf("%d %d", &m, &n),m||n) { p.clear(); for(int i=1;i<=m;i++) { G[i].clear(); scanf("%s",str); p[str]=i; } memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { scanf("%d %s", &x, s); int v=p[s]; dp[v]+=x; G[v].push_back(x); } int sum=0; for(int i=1;i<=m;i++) { int v1=dp[i]/2; memset(dp1,0,sizeof(dp1)); for(int k=0;k!=G[i].size();k++) { int v=G[i][k]; for(int j=v1;j>=v;j--) { dp1[j]=max(dp1[j-v]+v,dp1[j]); } } sum+=(dp[i]-dp1[v1]); } printf("%d\n",sum); } return 0;}
- POJ 3211Washing Clothes(01背包)
- POJ 3211 washing clothes (01背包)
- Washing Clothes poj 3211(01背包)
- POJ 3211 Washing Clothes(01背包)@
- poj 3211 Washing Clothes(01背包)
- POJ 3211 Washing Clothes【01背包】
- poj 3211 Washing Clothes(01背包问题)
- POJ 3211 Washing Clothes【01背包+STL】
- poj 3211 Washing Clothes(01背包)
- poj 3211 Washing Clothes 01背包
- poj 3211 Washing Clothes 01背包
- poj 3211 Washing Clothes 01背包问题
- poj 3211 Washing Clothes 分组01背包
- Washing Clothes - POJ 3211 01背包
- POJ 3211 Washing Clothes(01背包)
- POJ 3211 - Washing Clothes(01背包)
- poj 3211 Washing Clothes 01背包
- 01分组背包-poj 3211 Washing Clothes
- 引用传递
- 第三方库-Picasso
- 我所理解的设计模式——对象行为之命令(Comand)模式
- Java容易忽略的几个知识点!!!
- 【洛谷P3372】【模板】线段树 1
- POJ 3211 Washing Clothes(01背包)@
- SSH 小技巧大全
- AndroidStudio的使用技巧
- 详解原型链继承
- Java中无返回值的return语句使用总结
- 自考、起航!
- Leetcode #8 String to Integer (atoi)
- volatile关键字浅析
- 解决Windows下Python程序读取中文文件