题目链接
题目链接(可提交)
题意:
有n(1e2)种不同无限数量饮料,每种饮料提供x[i](1e2)水分,补充y[i](1e2)卡路里。
现在只选择一种饮料,整瓶整瓶喝,需要补充m(1e4)水分,并使得摄入卡路里最少。
问卡路里的最低摄入值,多组数据(1e2)
思路:
这一题读题很关键。一定要注意是只选一种饮料一直喝!
看清这个,一切就都很简单了,枚举每一种饮料,计算当补充满水分时所摄入的卡路里量,最后输出最小值即可。
代码:
/*
Author Owen_Q
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int main()
{
int t;
//freopen(".txt","r",stdin);
//ios::sync_with_stdio(false);
//cin.tie(0);
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int re = 1e7;
for(int i=0;i<n;i++)
{
int c,w;
scanf("%d%d",&c,&w);
int num = (m-1)/c + 1;
re = min(re,num*w);
}
printf("%d\n",re);
}
return 0;
}
误区:
这一题最大的误区就是,题目中的“选择一种饮料”很容易被忽视。容易被当成变相完全背包题,即达到背包容量条件下价值最小值。如果这样,这题的难度即会陡增。
如果这题按照这种错误理解,去除只喝一种饮料这个条件,则需要通过改进完全背包来解决。
将完全背包中的最大化背包价值改为最小化背包价值,然后枚举装满背包前提下所有可能的背包容量,最后对比选取最小价值即可
去条件后新题代码:
/*
Author Owen_Q
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 11000;
int w[maxn],c[maxn],f[maxn];
void bag(int v,int n)
{
int i,j;
f[0] = 0;
for(int j=v;j>=1;j--) f[j] = 1e7;
for(i=0;i<n;i++)
{
for(j=c[i];j<=v;j++)
{
if(f[j]>f[j-c[i]]+w[i])
f[j]=f[j-c[i]]+w[i];
}
}
return ;
}
int main()
{
int t;
//freopen(".txt","r",stdin);
//ios::sync_with_stdio(false);
//cin.tie(0);
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%d%d",&c[i],&w[i]);
bag(m+100,n);
int re = 1e7;
for(int i=m;i<=m+100;i++) re = min(re,f[i]);
printf("%d\n",re);
//cout << t << endl;
}
return 0;
}