博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3211 Washing Clothes[01背包问题]
阅读量:4968 次
发布时间:2019-06-12

本文共 2718 字,大约阅读时间需要 9 分钟。

转自:
Washing Clothes
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7063   Accepted: 2089

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

Source

, dearboy
 
 
 
感觉这个题目挺不错的!虽然自己还是没有做出来。
code:
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 using namespace std;20 21 int n,m; //m表示衣服的颜色种类,n表示衣服的件数 22 int dp[10001]; //1000*10+1 23 24 struct clothes //某一种颜色的结构体 25 {26 int num[1001],cnt,sum; //num表示洗每一件这种颜色的衣服的时间,cnt表示有几件衣服,sum表示总时间 27 char type[26]; // type表示这种衣服的颜色 28 }clo[26];29 30 int main()31 {32 int i,j,k;33 char temp[26];34 int num;35 int min;36 int ans;37 while(~scanf("%d%d",&m,&n),(m||n))38 {39 memset(clo,0,sizeof(clo));40 for(i=0;i
=clo[i].num[j];k--) //01背包从最大空间开始递减 62 {63 if(dp[k-clo[i].num[j]]+clo[i].num[j]>dp[k])64 dp[k]=dp[k-clo[i].num[j]]+clo[i].num[j];65 }66 }67 ans+=clo[i].sum-dp[clo[i].sum/2];68 }69 //-------------------------------DP-------------------------------------------------------------------------- 70 printf("%d\n",ans);71 }72 return 0;73 } 74 75 76 //注意数组的大小,WA了一次

 

转载于:https://www.cnblogs.com/XBWer/archive/2012/07/19/2599466.html

你可能感兴趣的文章
《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)
查看>>
nginx配置
查看>>
当人低潮时,如何提高情绪?
查看>>
django-cookiesession
查看>>
web应用程序上传文件 超过了最大请求长度
查看>>
一个数组:1,1,2,3,5,8,13,21...+m,求第30位数是多少?用递归实现;(常考!!!)...
查看>>
Network Saboteur(dfs)
查看>>
IdentityServer4-Resource定义-翻译
查看>>
.NET运行机制
查看>>
Mybatis使用Mybatis-generator插件及配置(数据库逆向工程)
查看>>
SpringBoot2.x整合Redis缓存自定义序列化
查看>>
20145219 gdb调试汇编堆栈分析
查看>>
django复习--什么是MTV模式
查看>>
iOS开发工具——网络封包分析工具Charles
查看>>
完整的温度转换程序
查看>>
循环cin读入如何终止
查看>>
Spring Framework--Data Access(1)--Transaction Management(2) - 声明式事务管理(2)
查看>>
假性进度效果
查看>>
团队项目:6.28日报
查看>>
MFC添加子对话框及如何初始化
查看>>