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