poj 3211 Washing Clothes(0/1背包)
来源:互联网 发布:iuni u3 用电信网络 编辑:程序博客网 时间:2024/05/17 01:51
点击打开链接poj 3211
思路: 0/1背包
分析:
1 题目要求洗完n件衣服所需的最少的时间,并且必须洗完一种颜色才能跳到下一种
2 仔细想想这一题和uva上面的一道分金币非常的相似,由于要求必须洗完一种颜色才能跳到下一种,那么我们只要使得洗每一种颜色的时间最少那么总的就最少,那怎样才能够最少的时间呢?也就是要求两个人的时间差最小(也就相当于一个人用一半的时间去能够洗的最多的衣服),那么就转化为0/1背包的思想
3 做m次的dp然后求和即可
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N = 15;const int MAXN = 100010;int m , n;int w[N][N] , pos[N];int sum[N] , dp[MAXN];char color[N][N];int returnIndex(char *str){ for(int i = 1 ; i <= m ; i++) if(!strcmp(str , color[i])) return i;}int solve(){ int ans = 0; for(int i = 1 ; i <= m ; i++){ memset(dp , 0 , sizeof(dp)); for(int j = 1 ; j <= pos[i] ; j++) for(int k = sum[i]/2 ; k >= w[i][j] ; k--) dp[k] = max(dp[k] , dp[k-w[i][j]]+w[i][j]); ans += sum[i]-dp[sum[i]/2]; } return ans;}int main(){ char str[N]; int x; while(scanf("%d%d" , &m , &n) && n+m){ for(int i = 1 ; i <= m ; i++) scanf("%s" , color[i]); memset(pos , 0 , sizeof(pos)); memset(sum , 0 , sizeof(sum)); for(int i = 1 ; i <= n ; i++){ scanf("%d %s" , &x , str); int index = returnIndex(str); pos[index]++; sum[index] += x; w[index][pos[index]] = x; } printf("%d\n" , solve()); } return 0;}
- poj 3211 Washing Clothes(0/1背包)
- POJ 3211 Washing Clothes (0/1背包)
- POJ 3211 Washing Clothes 0-1背包
- poj 3211 Washing Clothes 0-1背包
- poj 3211 Washing Clothes【0-1背包】
- POJ 3211 Washing Clothes (分组背包+0/1背包)
- poj 3211 Washing Clothes(背包)
- 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背包
- POJ 3211Washing Clothes(01背包)
- POJ 3211 Washing Clothes 背包题解
- Washing Clothes - POJ 3211 01背包
- UNIX网络编程卷一 笔记 第二章 传输层
- sprintf函数以及其返回值
- 导航栏
- struts1.x知识点
- poj 2186 Popular Cows
- poj 3211 Washing Clothes(0/1背包)
- 对任务栏的操作
- poj 2506-计算方格摆放的方法数
- Chrome for android 编译
- C++菜鸟常见错误整理(第十四天)
- c++ 笔试题
- 面试题解析:3 java中 == 和 equals的区别
- 贝叶斯、概率分布与机器学习
- VC6.0file->open出错解决办法