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

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

转自:
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了一次

 

转载地址:http://agkil.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
Spark技术互动问答分享
查看>>
开机自动挂载与autofs触发挂载
查看>>
用JS写出星空
查看>>
Windows任务管理器
查看>>
SSDP:DDoS***的“新宠”
查看>>
为Android模拟器配置代理
查看>>
输入一个整数,计算它各位上数字的和。(注意:是任意位的整数)
查看>>
微信公众平台开发:进阶篇(Web App开发入门)
查看>>
cocos2d 字体颜色
查看>>
思科/华为/Juniper探测技术
查看>>
C程序的内存分布
查看>>
Activiz 使用笔记-4 数据源(2)
查看>>
第一堂TCP/IP课
查看>>
JNI的静态注册
查看>>
关于nagios 邮件报警问题
查看>>
hibernate 框架学习笔记
查看>>
HTTPS 使用certbot升级过期证书
查看>>
PIX的应用案例
查看>>
理想的工作永远不存在
查看>>