分组背包例题

NIH Budget

https://ac.nowcoder.com/acm/contest/12794/E

题目:此题总费用固定,同种疾病的费用只能选择一种方案(每种疾病有四种方案)(方案:花多少钱救多少人),求最多挽救人数
分组背包经典例题

#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int const N=1e4+7;
int const M=1e5+7;
int t,n,m;
int v[N],w[N];
int f[M];
int main(){
    cin >> t;
    for(int p=1;p<=t;++p){
        cin >> n >> m;
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;++i){
            for(int j=1;j<=4;++j){
                cin >> v[(i-1)*4+j] >> w[(i-1)*4+j];
            }
        }
        for(int k=1;k<=n;++k){
            for(int j=m;j>=0;--j){
                for(int i=1;i<=4;++i){
                    int z=(k-1)*4+i;
                    if(j>=v[z]) 
                      f[j]=max(f[j],f[j-v[z]]+w[z]);
                }
            }
        }
        cout << "Budget #" << p << ": Maximum of " ;
        cout << f[m] <<" lives saved.\n\n";
    }
    return 0;
}
全部评论

相关推荐

未知的命运:大佬这都找不到我还找啥啊
点赞 评论 收藏
分享
10-30 16:31
重庆大学 Java
代码飞升:你说你善于学习,大家都会说。你说你是985,985会替你表达一切
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务