01揹包问题 C++

原文地址http://blog.csdn.net/mu399/article/details/7722810

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>

using namespace std;

//动态规划 之 01揹包问题
class PackageItem
{
public:
    char name;
    int weight;
    int value;
    PackageItem(char n,int w,int v):name(n),weight(w),value(v){};
    PackageItem(){};
};

char namearray[] = {'a','b','c','d','e'};
int weightarray[] = {2,2,6,5,4};
int valuearray[] = {6,3,5,4,6};

int get01PackageAnswer(PackageItem* bagItems,int itemsize, int bagsize, vector<char> &answer)
{
    int i,j;
    int **bagMatrix = new int*[itemsize];
    for(i = 0; i < itemsize; i++)
        bagMatrix[i] = new int[bagsize+1];
    PackageItem item;
    for(i = 1; i <= bagsize; i ++)
    {
        for(j = 0 ; j < itemsize; j++)
        {
            item = bagItems[j];

            if(item.weight > i)  //此时容量为i的揹包装不下第 j个物品
            {
                if(j == 0)
                    bagMatrix[j][i] = 0;
                else
                    bagMatrix[j][i] = bagMatrix[j-1][i]; 
            }
            else   //将第j个物品装入揹包
            {
                if(j == 0)
                {
                    bagMatrix[j][i] = item.value;
                }
                else
                {
                    int compvalue = bagMatrix[j-1][i - item.weight] + item.value;
                    bagMatrix[j][i] = max(compvalue,bagMatrix[j-1][i]) ;
                }

            }
        }

    }
    for(i = 0 ; i < itemsize; i++)
        {
            for(j = 1; j < bagsize+1 ; j++)
                cout<< bagMatrix[i][j]<<" ";
            cout<<endl;
        }

    int cursize = bagsize;
    for(i = itemsize -1 ;i >= 0; i --)
    {
        item = bagItems[i];
        if(cursize == 0)
            break;
        if(i == 0 && cursize > 0)  //考虑到揹包大小 有容余 并且 i = 0时 ,i-1会越界
        {
            answer.push_back(item.name);
            break;
        }
        if(curval > task[i])
        {
            if(bagMatrix[i][cursize] - bagMatrix[i-1][cursize - item.weight] == item.value)
            {
                answer.push_back(item.name);
                cursize -= item.weight;
            }
        }
    }


    return bagMatrix[itemsize - 1][bagsize];
}




void main()
{
    int leng = sizeof(namearray)/sizeof(char);
    PackageItem* bagItems = new PackageItem[leng];  //创建数组必须要用默认构造函数
    int i;
    for(i = 0; i < leng ; i++)
    {
        PackageItem bag(namearray[i],weightarray[i],valuearray[i]);
        bagItems[i] = bag ;
    }
    vector<char> ans;
    int va = 0;
    va = get01PackageAnswer(bagItems,leng,10,ans);
    system("pause");
}

//网易 2017笔试 :双核CPU同时处理 多个任务,每个CPU一次处理一个任务 ; 每个任务大小都是1kb的倍数,问最短需要多久处理完
// 输入任务个数n:5 ; 1 <= n <= 50
//每个任务大小:3072 3072 7168 3072 1024 1024<=length[i]<=419430
//输出处理时间: 9216

void main()
{
    int n = 5;
    int task[] = {3072,3072,7168,3072,1024};
    int bagsize = 0;
    for(int i = 0 ;i < n ;i ++)
    {
        task[i] = task[i]/1024;
        bagsize += task[i];
    }
    int totalsize = bagsize;
    bagsize /= 2.0;

    int cursize;
    int i,j;
    vector<vector<int>> bagMatrix(n,vector<int> (bagsize + 1));

    for(i = 0; i < n; i++)
    {
        for(j = 1; j <= bagsize; j++)
        {
            if(task[i] > j)
            {
                if(i == 0)
                {
                    bagMatrix[i][j] = 0;
                }
                else
                {
                    bagMatrix[i][j] = bagMatrix[i-1][j];
                }
            }
            else
            {
                if(i == 0)
                {
                    bagMatrix[i][j] = task[i];
                }
                else
                {
                    int comval = bagMatrix[i-1][j - task[i]] + task[i];
                    bagMatrix[i][j] = max(comval,bagMatrix[i-1][j]);
                }
            }
        }
    }
    int curval = bagMatrix[n-1][bagsize];

    vector<int> itemcomb;
    for(i = n - 1 ; i >= 0; i--)
    {
        if(curval == 0)
            break;
        if(i == 0 && curval > 0)
        {
            itemcomb.push_back(i);
            break;
        }
        if(curval > task[i])
            if(bagMatrix[i][curval] - bagMatrix[i-1][curval - task[i]] == task[i])
            {
                itemcomb.push_back(i);
                curval -= task[i];
            }
    }



    int newval = bagMatrix[n-1][bagsize];
    int  val = max(newval,(totalsize - newval)) *1024;
    cout<< val;
    system("pause");

}
点赞