背包DP汇总

A - Bone Collector


Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … 
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ? 

Input
The first line contain a integer T , the number of cases. 
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 2  31).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
     

题意是:给你N个数,体积不超过V。 比如例子给的 5 10  5个数  下面一行给的是体积  最下面给的是价值  求不超过V的情况下的价值最大是多少。

直接上代码:
#include <bits/stdc++.h>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e6+5;


int dp[maxn],w[maxn],v[maxn];
int main()
{
    int T,n,V;
    cin>>T;
    while(T--)
    {
        cin>>n>>V;
        me(dp);
        me(w);
        me(v);
        for(int i=1;i<=n;i++)
            cin>>w[i];
        for(int i=1;i<=n;i++)
            cin>>v[i];
        for(int i=1;i<=n;i++)
       {
          for(int j=V;j>=v[i];j--)
             dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
       }
        cout<<dp[V]<<endl;
    }
}


/*
1
5 10
1 2 3 4 5
5 4 3 2 1

*/

B - 饭卡

 

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。 
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。 
Input
多组数据。对于每组数据: 
第一行为正整数n,表示菜的数量。n<=1000。 
第二行包括n个正整数,表示每种菜的价格。价格不超过50。 
第三行包括一个正整数m,表示卡上的余额。m<=1000。 

n=0表示数据结束。 
Output
对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
Sample Input
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0
Sample Output
-45
32
题意:第三行为你的饭卡余额 第二行为菜的价格  求买了菜之后余额最小是多少 。
代码:
#include <bits/stdc++.h>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e6+5;


int a[maxn],d[maxn];
int main()
{
	int n,m,i,j,vmax=0;
	while(cin>>n,n)
	{
		memset(d,0,sizeof(d));
		vmax=0;
		for( i=0;i<n;i++)
		{
			cin>>a[i];
		}
		cin>>m;
		sort(a,a+n);
		int m1=m-5;// 由题意  直接减5先判断是大于5 还是小于5
		if(m1<0)
		{
			cout<<m<<endl;
			continue;
		}
		else
		{
			for( i=0;i<n-1;i++)// i为有多少个数
			{
				for( j=m1;j>=a[i];j--)// j为值是多少
				{
					d[j]=max(d[j],d[j-a[i]]+a[i]);
				}
			}
			printf("%d\n",m-d[m1]-a[n-1]);//最后剩余的钱
		}
	}
}
/*
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0   */

C - CD

 
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on
CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How
to choose tracks from CD to get most out of tape space and have as short unused space as possible.
Assumptions:
• number of tracks on the CD does not exceed 20
• no track is longer than N minutes
• tracks do not repeat
• length of each track is expressed as an integer number
• N is also integer
Program should find the set of tracks which fills the tape best and print it in the same sequence as
the tracks are stored on the CD
Input
Any number of lines. Each one contains value N, (after space) number of tracks and durations of the
tracks. For example from first line in sample data: N = 5, number of tracks=3, first track lasts for 1
minute, second one 3 minutes, next one 4 minutes
Output
Set of tracks (and durations) which are the correct solutions and string ‘sum:’ and sum of duration
times.
Sample Input
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
Sample Output
1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45

题意:一行为一个例子  第一个是体积最大  第二个数是有多少个  后面几个为体积。
这个麻烦主要是麻烦在要求出用哪个加上去的。 所以想的话就是用标记。  想一想同位素标记法。 
所以在背包判断的时候做一个标记
#include <bits/stdc++.h>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e6+5;

int main()
{
    int m, n, cd[25], dp[maxn];
    int vis[25][maxn];
    while (cin>>m>>n)
    {
        int i, j;
        for (i=1; i<=n; i++)
            cin>>cd[i];
        me(dp);me(vis);
        for (i=n; i>=1; i--)
        {
            for (j=m; j>=cd[i]; j--)
            {
                if (dp[j] < dp[j-cd[i]] + cd[i])
                {
                    dp[j] = dp[j-cd[i]] + cd[i];
                    vis[i][j] = 1;
                }
            }
        }
        for (i=1, j=dp[m]; i<=n, j>0; i++)
        {
            if (vis[i][j])
            {
                printf("%d ", cd[i]);
                j -= cd[i];
            }
        }
        printf("sum:%d\n", dp[m]);
    }
    return 0;
}


/*
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
*/



 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值