UVALive - 7197 (完全背包+位运算枚举)

You are running a machine shop producing custom axles for radio-controlled cars. These axles can be manufactured from either steel or stainless steel, with the stainless steel parts usually priced differently from plain steel. You have two CNC lathes: one is configured to produce stainless steel parts, and the other is configured to produce steel parts. In a given production run, a specific part design can only be assigned to one of the two machines, i.e., the same part design can be manufactured in either steel or stainless steel, but not both. The parts are produced from solid rods of material; today your stock levels are pretty low and you only have one length of steel rod, and one length of stainless steel rod. Part designs are selected from a library, where each part is listed according to the length of stock material required to produce it, as well as the current profit yield of the part in both steel and stainless steel versions. You must select a subset of parts from your library, and decide how many copies of those parts to produce, as well as whether a given part should be produced in steel or stainless steel, in a way that maximises your profit given your available material stock. Input Your input consists of an arbitrary number of records, but no more than 30. Each record starts with an integer n, with 2 ≤ n ≤ 14, denoting the number of parts available in your library. The next n lines each provides three positive integer values, m p s where m denotes the length of stock material (in millimetres) consumed when producing one of these parts, p denotes the profit made when manufacturing the part from steel (in Rands), and s denotes the profit made when manufacturing the part in stainless steel, subject to the constraints 1 ≤ m ≤ 1500 and 1 ≤ p, s ≤ 1000. After these n lines follows another line containing two positive integer values, q r where q denotes the length of steel stock you have available (in millimetres), and r denotes the length of stainless steel stock you have available, subject to 1 ≤ q ≤ 1000 and 1 ≤ r ≤ 1200. The end of input is indicated by a line containing only the value ‘-1’. Output For each input record, output u where u denotes the maximum profit to be made with the available stock material. Sample Input 3 10 200 300 20 300 200 10 200 600 200 100 13 280 306 484 185 335 212 38 110 40 96 236 124 256 478 279 256 301 480 249 292 445 258 299 437 281 287 472 44 62 142 349 369 593 258 271 466 208 406 243 998 1123 -1 Sample Output 10000 6410

 

利用位运算枚举像取 1 2 3 4. .....n 中取哪几个数的情况,避免了用深搜

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 2000
#define INF 0x3f3f3f3f
struct Node
{
    int m;
    int p;
    int s;
}a[20];
int dp1[MAXN];
int dp2[MAXN];
int vis[MAXN];
int n;
int q,r;
int ans;
int solve(int x)
{
     int i,j;
     memset(dp1,0,sizeof(dp1));
     memset(dp2,0,sizeof(dp2));
     for(i=1;i<=n;i++)
     {
         if(x&1)
         {
             for(j=a[i].m;j<=q;j++)
             dp1[j]=max(dp1[j],dp1[j-a[i].m]+a[i].p);
         }
         else
         {
            for(j=a[i].m;j<=r;j++)
            dp2[j]=max(dp2[j],dp2[j-a[i].m]+a[i].s);
         }
         x>>=1;
     }
     return dp1[q]+dp2[r];
}
int main()
{
    int i;
    while(scanf("%d",&n),n!=-1)
    {
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
          int m,p,s;
          scanf("%d%d%d",&m,&p,&s);
          a[i].m=m;
          a[i].p=p;
          a[i].s=s;
        }
        scanf("%d%d",&q,&r);
        ans=0;
        for(i=0;i<(1<<n);i++)
        ans=max(ans,solve(i));
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值