01背包

来源:互联网 发布:生久网络 编辑:程序博客网 时间:2024/04/28 01:42

Packets

A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.

Input

The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.

Output

The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.

把一些不同大小的产品装进同一个规格的箱子里面,只考虑平面,先把比较大的装进去,2*2和1*1的用来填空。

#include <iostream>#include<cstdio>using namespace std;int main(){    int a[10];    while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))    {        if(!(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]))            break;        int f2[4]={0,5,3,1};        int ans=a[6]+a[5]+a[4]+(a[3]+3)/4;        int y=a[4]*5+f2[a[3]%4];        if(a[2]>y)            ans+=(a[2]-y+8)/9;        int x=ans*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;        if(a[1]>x) ans+=(a[1]-x+35)/36;        printf("%d\n",ans);    }    return 0;}


采药【(0-1)背包问题】

辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。” 如果你是辰辰,你能完成这个任务吗?
输入的第一行有两个整数T(1 <= T <= 1000)和M(1 <= M <= 100),用一个空格隔开,T代表总共能够用来采药的时间,M代表山洞里的草药的数目。接下来的M行每行包括两个在1到100之间(包括1和100)的整数,分别表示采摘某株草药的时间和这株草药的价值。
输出包括一行,这一行只包含一个整数,表示在规定的时间内,可以采到的草药的最大总价值。
#include <iostream>#include<cstdio>#include<cstring>using namespace std;struct caoyao{    int t,m;}a[105];int main(){    int T,M,f[105][1005];    while(~scanf("%d%d",&T,&M))    {        for(int i=1;i<=M;i++)            scanf("%d%d",&a[i].t,&a[i].m);            memset(f,0,sizeof(f));        for(int i=1;i<=M;i++)        {            if(a[i].t>T) continue;            for(int j=0;j<=T;j++)            {            if(j>=a[i].t)                f[i][j]=max(f[i-1][j],f[i-1][j-a[i].t]+a[i].m);                             //放与不放第i个草药的价值取大的            else f[i][j]=f[i-1][j];            }        }        printf("%d\n",f[M][T]);    }    return 0;}


开心的金明【(0-1)背包问题】

金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”。今天一早金明就开始做预算,但是他想买的东西太多了,肯定会超过妈妈限定的N元。于是,他把每件物品规定了一个重要度,分为5等:用整数1~5表示,第5等最重要。他还从因特网上查到了每件物品的价格(都是整数元)。他希望在不超过N元(可以等于N元)的前提下,使每件物品的价格与重要度的乘积的总和最大。设第j件物品的价格为v[j],重要度为w[j],共选中了k件物品,编号依次为j1,j2,……,jk,则所求的总和为:v[j1]*w[j1]+v[j2]*w[j2]+ …+v[jk]*w[jk]。(其中*为乘号)请你帮助金明设计一个满足要求的购物单。
输入文件的第1行,为两个正整数,用一个空格隔开:N  m(其中N(<30000)表示总钱数,m(<25)为希望购买物品的个数。)从第2行到第m+1行,第j行给出了编号为j-1的物品的基本数据,每行有2个非负整数v  p(其中v表示该物品的价格(v<=10000),p表示该物品的重要度(1~5))
输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值(<100000000)。
与上一道题的区别就是加重要度与价格的乘积
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int dp[30][30010];struct goods{    int v,p;}a[30];int main(){    int N,M;    while(~scanf("%d%d",&N,&M))    {        for(int i=1;i<=M;i++)        {            scanf("%d%d",&a[i].v,&a[i].p);        }        memset(dp,0,sizeof(dp));        for(int i=1;i<=M;i++)            for(int j=0;j<=N;j++)        {            if(j>=a[i].v)                dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i].v]+a[i].v*a[i].p);                       //加的数是重要度与价格的乘积            else dp[i][j]=dp[i-1][j];        }        printf("%d\n",dp[M][N]);    }    return 0;}


0 0
原创粉丝点击