PAT 算法笔记P271 DFS 不可分割背包问题 P273 DFS 选择K个数

P271 DFS 不可分割背包问题

题目描述:
有n件物品,每件物品的重量为w[i], 价值为c[i]。现在需要选出若干件物品放入一个容量为v的背包中,使得在选入背包的物品重量和不超过容量v的前提下,让背包中物品的价格之和最大,求最大价值。

示例:

输入:
5 8 //5件物品,背包容量为8
3 5 1 2 2  //物品重量
4 5 2 1 3 //物品价值
 
输出:10

code

/*算法笔记P272 不可分割背包问题
 *
 */
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <math.h>
#include<cstdio>
#include<iostream>
#include<string.h>
#include<map>
#include<stack>
#include<sstream>
using namespace std;

const int maxn = 30;
int n,V,maxValue = 0;//物件品数n,背包容量V,最大价值maxValue
int w[maxn], c[maxn];//w[i]为每件物品的重量,c[i]为每件物品的重量

//DFS,index为当前处理的物品编号
//sumW和sumC分别为当前总重量和当前总价值
void DFS(int index, int sumW, int sumC){
    if(index==n){//已经完成了对n件物品的选择(死胡同) 
        if(sumW<=V&&sumC>maxValue){
            maxValue = sumC;//不超过背包容量时更新最大价值maxValue
        }
        return;
    }

    //岔道口
    DFS(index+1, sumW, sumC);//不选第index件物品
    DFS(index+1, sumW + w[index], sumC + c[index]);//选第index件物品
}

int main(){
    scanf("%d%d",&n,&V);
    for(int i = 0;i<n;++i){
        scanf("%d",&w[i]);//每件物品的总重量
    }

    for(int i = 0;i<n;++i){//每件物品的价值
        scanf("%d",&c[i]);
    }

    DFS(0,0,0);//初始时为第0件物品,当前总重量和总价值均为0
    printf("%d\n",maxValue);
    return 0;
}

第2版 优化
设置sumW+w[index]<=V的 选择条件

//DFS,index为当前处理的物品编号
//sumW和sumC分别为当前总重量和当前总价值
void DFS(int index, int sumW, int sumC){
    if(index==n){//已经完成了对n件物品的选择(死胡同) 
        if(sumW<=V&&sumC>maxValue){
            maxValue = sumC;//不超过背包容量时更新最大价值maxValue
        }
        return;
    }

    //岔道口
    DFS(index+1, sumW, sumC);//不选第index件物品

    // 只有加入第index件物品后未超过容量V,才能继续
    if(sumW + w[index]<=V){
        if(sumC + c[index] > maxValue){
            maxValue = sumC + c[index];//更新最大价值maxValue
        }
        DFS(index+1, sumW + w[index], sumC + c[index]);//选第index件物品
    }
 
}

P273 枚举从N个整数中选择K个数的所有方案

给定N个整数(可能有负数),从中选择K个数,使得K个数之和恰好等于一个给定的整数X;如果有多种方案,选择它们中元素平方和最大的一个。

#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <math.h>
#include<cstdio>
#include<iostream>
#include<string.h>
#include<map>
#include<stack>
#include<sstream>
using namespace std;
const int maxn = 100000;
//序列A中n个数选k个数使得和为x,最大平方和为maxSumSqu
int n,k,x,maxSumSqu = -1, A[maxn];

//temp存放临时方案,ans存放平方和最大的方案
vector<int> temp, ans;

//当前处理index号整数,当前已选整数个数为nowK
//当前已选整数之和为sum, 当前已选整数平方和为sumSqu
void DFS(int index, int nowK, int sum, int sumSqu){
    if(nowK==k && sum==x){//找到k个数
        if(sumSqu>maxSumSqu){//如果比当前找到的更优
            maxSumSqu = sumSqu;//更新最大平方和
            ans = temp;//更新最优方案
        }
        return;
    }

    //已经处理完n个数,或者超过k个数,或者和超过x,返回
    if(index==n||nowK>k||sum>x) return;
    //选index号数
    temp.push_back(A[index]);
    DFS(index+1, nowK+1, sum+A[index], sumSqu+A[index]*A[index]);

    temp.pop_back();
    //不选index号数
    DFS(index+1, nowK, sum, sumSqu);
}

int main() {
    cin >> n >> k >> x ;
    for (int i = 0;i < n;i++) {
        scanf("%d", &A[i]);
    }
    DFS(0,0, 0,0);
    vector <int> ::iterator it;
    it = ans.begin();
    printf("%d\n", maxSumSqu);
    printf("%d %d\n", *it ,*(it+1));

    return 0;
}

当问题修改为每一个数可以被选择多次,那么选择K个数,使得K个数之和恰好为X。只需要把“选index号数"这条分支的代码修改为

DFS(index, nowK+1, sum+A[index],sumSqu + A[index]*A[index])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值