【Algothrim】动态规划实例 (PC Assemble)

来源:互联网 发布:python 时间戳 整数 编辑:程序博客网 时间:2024/04/29 17:23


Given below are the raw materials qunatities and their respective selling price(if sold as raw).
  D --> No of CPUs
  E --> No of memory chips
  F --> No of boards
  d --> Selling price of CPU
  e --> Selling price of Memory chips
  

We are given N Computer configurations like below:
Di, Ei, Fi, SPi, which are the CPU, Chips, Boards and one unit selling price for ith Computer respectively.

Our task is to maximize the final cost.


Constraints:
1. Can use at Max 3 different Configurations
2. We can use 1 configuration multiple times
3. Remaining Inventories can be sold on its selling price

 

Input:
T --> Number of test cases.
D E F d e --> Inventories
N   --> Total Configuration Count
Di Ei Fi SPi
...
Dn En Fn SPn

 

1<=T<=10

1<= D, E, F <= 100

1<= d, e <=100000

1<=N<=8

 

Output:

First Line print the Case #testCaseNumber

Second Line Print Maximum Cost per test case in each line.

 

 

Sample Input:

1    --> Total Test Case
10 10 10 2 1  --> D E F d e
1    --> PC Configuration Count
1 2 2 3   --> D1 E1 F1 SP1

 

Sample Output:

Case #1

30



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAXN  10
#define MAXD  102
#define MAXE  102
#define MAXF  102
#define rint  register int

struct computer {
 int D,E,F,SP;
};

struct point {
 int value;
 char count,config[MAXN];
};

int Ans,N,D,E,F,sd,se;
struct computer C[MAXN];
struct point dp[MAXD][MAXE][MAXF];

int find(struct point p, int x)
{
 int i;
 for (i=1;i<=p.count;i++)
 if (p.config[i]==x) return 1;
 return 0;
}

void copy_config(struct point *p, struct point *q)
{
 int i;
 p->count=q->count;
 for (i=1;i<=p->count;i++)
 p->config[i]=q->config[i];
}

int main(void)
{
 int T,cd,ce,cf,csp;
 rint i,j,k,x,t,count,a,b,c,d,v;
 setbuf(stdout,NULL);scanf("%d",&T);
 for (t=1;t<=T;t++) {
  scanf("%d %d %d %d %d\n%d",&D,&E,&F,&sd,&se,&N);
  for (i=j=0;i<N;i++) {
   scanf("%d %d %d %d",&cd,&ce,&cf,&csp);
   if (cd>D || ce>E || cf>F) continue;
   C[++j].D=cd;C[j].E=ce;C[j].F=cf;C[j].SP=csp;
  }
  N=j;Ans=0;
  for (i=0;i<=D;i++)
  for (j=0;j<=E;j++)
  for (k=0;k<=F;k++) {
   dp[i][j][k].value=(i*sd+j*se);
   dp[i][j][k].count=0;
  }

  for (x=1;x<=N;x++)
  for (i=C[x].D;i<=D;i++)
  for (j=C[x].E;j<=E;j++)
  for (k=C[x].F;k<=F;k++) {
   a=i-C[x].D;b=j-C[x].E;
   c=k-C[x].F;d=C[x].SP;
   v=dp[a][b][c].value+d;
   if (dp[i][j][k].value>v) continue;
   if (dp[i][j][k].value<v) {
    dp[i][j][k].value=v;
    copy_config(&dp[i][j][k],&dp[a][b][c]);
    if (!find(dp[a][b][c],x)) {
     count=++dp[i][j][k].count;
     dp[i][j][k].config[count]=x;
    }
    continue;
   }
   if (dp[i][j][k].count>dp[a][b][c].count)
    copy_config(&dp[i][j][k],&dp[a][b][c]);
  }

  for (i=0;i<=D;i++)
  for (j=0;j<=E;j++)
  for (k=0;k<=F;k++) {
   if (dp[i][j][k].count>3) continue;
   v=dp[i][j][k].value+((D-i)*sd+(E-j)*se);
   if (Ans<v) Ans=v;
  }
  printf("Case #%d\n%d\n",t,Ans);
 }
 return 0;
}

0 0
原创粉丝点击