现在的位置: 首页 > 算法 > 正文

poj 3211 Washing Clothes(0/1背…

2019年04月02日 算法 ⁄ 共 1288字 ⁄ 字号 评论关闭
题意:有n(1~10)种不同颜色的衣服总共m
(1~100)件,Dearboy和她的girlfriend两个人要一起洗完全部衣服,
两个人洗衣速度相同,并且已知每件衣服需要的时间<1000)。
两个人可以同时洗衣。为了预防色彩混合,必须一种颜色的衣服洗完之后,两个人才能开工洗下一种颜色的衣服,问两个人洗完所有的衣服需要的最短时间。

思路:每种颜色的衣服可以分开来考虑,算出每种颜色的衣服所需要的最短时间,最后加起来即可。
然后再来考虑单一一种颜色的衣服该怎么洗,考虑到可能一个人要多洗一会儿,一个人要少洗一会儿,
两个人所花的时间越接近一个人洗时总时间的一半,肯定是越好的方案,所以,用一个人洗时总时间的一半做容量,所有的衣服所花的时间做容量与价值(容量等于价值),转化成n个01背包,
用01背包求出最大价值就是花时间少的那个人所用的时间了,
然后用总时间减去这个花时间少的人的用时,就得到了用时多的人的用时了。

这回把代码写的太乱了,唉~ 就当到此一游吧,不注释了。。。

//208K   
16MS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 20
#define N 150
using namespace std;
struct data
{
    int
time;
    char
col[M];
}node[N];
int max (int a, int b)
{
    return a
> b? a : b;
}
bool cmp (data a,data b)
{
    if (strcmp
(a.col,b.col) >= 0)
       
return true;
    return
false;
}
int main ()
{
    int
n,m,i,j,k,ans;
    int
t[N],dp[10000];
    char
str[N+10];
    while (scanf
("%d%d",&m,&n))
    {
       
if (m == 0&&n == 0)
           
break;
       
getchar ();
       
gets (str);

       
for (i = 0;i < n;i ++)
           
scanf ("%d %s",&node[i].time,node[i].col);
       
sort (node,node + n,cmp);
       
strcpy (node[n].col,"##");
       
node[n++].time = 0;
       
strcpy (str,node[0].col);
       
k = 0;
       
ans = 0;
       
for (i = 0;i < n;i ++)
       
{
           
if (strcmp (str,node[i].col) == 0)
           
{
               
t[k++] = node[i].time;
           
}
           
else
           
{
               
m = k;
               
int p,q;
   

抱歉!评论已关闭.