@Alpacadh
2019-02-25T23:47:02.000000Z
字数 1977
阅读 749
背包
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<stdio.h>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e4+5;
const double eps=1e-4;
typedef long long ll;
typedef pair<int,ll> pil;
int dp[N];
int a[N];
int main()
{
int t;
scanf("%d",&t);
int tot=1;
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=m-1;j>=a[i];j--)
{
dp[j]=max(dp[j],dp[j-a[i]]+1);
}
}
int ans=0,pos=0;
for(int i=m-1;i>=0;i--)
{
if(dp[i]>ans)
{
ans=dp[i];
pos=i;
}
}
printf("Case %d: %d %d\n",tot++,ans+1,pos+678);
}
return 0;
}
参考https://blog.csdn.net/u013007900/article/details/38512039。
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<stdio.h>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=2e4+5;
const double eps=1e-4;
typedef long long ll;
typedef pair<int,ll> pil;
int dp[N];
int v[N],w[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
m-=n;
int tt;
scanf("%d",&tt);
for(int i=0;i<tt;i++)
{
scanf("%d%d",&v[i],&w[i]);
}
memset(dp,inf,sizeof(dp));
dp[0]=0;
for(int i=0;i<tt;i++)
{
for(int j=w[i];j<=m;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[m]>=inf)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m]);
}
return 0;
}