最大报销额 HDU - 1864

本文深入探讨了背包问题的解决策略,特别是在每类物品有限额的情况下。通过详细的代码示例,展示了如何使用动态规划来找到最优解,确保理解背包问题的核心算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本就是个背包,题意有误是每类物品限额

#include<bits/stdc++.h>
using namespace std;
#define fst first
#define sec second
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> P;
const int MAX_N = 510;
const int MAX_M = 10000010;
bool dp[MAX_M];
int vals[60],cnt;
char s[1000];
int main() {
    double mt;
    int M,N;
    while (~scanf("%lf%d",&mt,&N)) {
        M = mt * 100;
        if (N == 0) break;
        int mx = 0;
        mem(dp,0);
        cnt = 0;
        dp[0] = true;
        for (int i  =0 ;i < N;i++) {
            int c2;
            sci(c2);
            int sum = 0;
            int loc = 0;
            bool flag = true;
            int tempa = 0,tempb = 0,tempc = 0;
            for (int j = 0;j < c2;j++) {
                double t;
                int num;
                scanf("%s",s);
                sscanf(s + 2,"%lf",&t);
                num = 100 * t;
                sum += num;
                if (s[0] != 'A' && s[0] != 'B' && s[0] != 'C') {
                    flag = false;
                }
                if (s[0] == 'A') tempa += num;
                if (s[0] == 'B') tempb += num;
                if (s[0] == 'C') tempc += num;
            }
            if (sum > 100000) flag = false;
            if (tempa > 60000) flag = false;
            if (tempb > 60000) flag = false;
            if (tempc > 60000) flag = false;
            if (flag) {
                vals[cnt++] = sum;
            }
        }
        for (int i = 0;i < cnt;i++) {
            for (int j = M;j >= vals[i];j--) {
                if (dp[j - vals[i]]) dp[j] =  true;
            }
        }
        for (int j = M;j >= 0;j--) {
            if (dp[j]) {
                mx = j;
                break;
            }
        }
        printf("%.2lf\n",(double) mx /100);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值