明明的预算方案-动态规划

来源:互联网 发布:php datezone 编辑:程序博客网 时间:2024/04/30 15:30

http://www.rqnoj.cn/Problem_6.html

总结

1、这是一个有依赖关系的动态规划,根据动态规划的无后效性,决策特点,采用了A,AB,AC,ABC形式

2、++length = length-2 + i,这类语言细节,还是很容易错误,应当尽量避免

3、这题自个写的比较绕,效率也低,纯粹是为了练习自己的思维能力和提升编码正确率

4、这题只有2个得分点,郁闷了又

 

 

#include <iostream>
using namespace std;
int totalMoney,totalArticle;
struct {
 int value;
 int depth;
 int father;
    //bool isBought;
 int leftChild;
 int rightChild;
}article[61],articleInput[200];
int max[200][32001];
int length;

int Big(int x, int y)
{
 return x>y ? x : y;
}
int main()
{
 int i,j;
 cin>>totalMoney>>totalArticle;
 for (i=1; i<=totalArticle; i++)
 {
  cin>>article[i].value>>article[i].depth>>article[i].father;
  //article[i].isBought = false;
  if (article[i].father)
  {  
   int fatherId = article[i].father;
   if(!article[fatherId].leftChild) article[fatherId].leftChild = i;
   else article[fatherId].rightChild = i;
  }
 }

 for (i=1; i<=totalArticle; i++)
 {
  //cout<<i<<": "<<article[i].father<<' '<<article[i].leftChild<<' '<<article[i].rightChild<<' '<<endl;
 }

 length = 0;
 //增加依赖性,更新数组
 for (i=1; i<=totalArticle; i++)
 {
  if (!article[i].father)
  {
   articleInput[++length].value = article[i].value;
   articleInput[length].depth = article[i].depth*article[i].value;

   //cout<<length<<": "<<articleInput[length].value<<"  "<<articleInput[length].depth<<endl;
 // }
 // else
 // {
   if (article[i].leftChild)
   {
    articleInput[++length].value = article[article[i].leftChild].value + article[i].value;
    articleInput[length].depth = article[article[i].leftChild].value*article[article[i].leftChild].depth + article[i].value*article[i].depth;
   
    //cout<<length<<": "<<articleInput[length].value<<"  "<<articleInput[length].depth<<endl;
    
    if (article[i].rightChild)
    {
     articleInput[++length].value = article[article[i].rightChild].value + article[i].value;
     articleInput[length].depth = article[article[i].rightChild].value*article[article[i].rightChild].depth + article[i].value*article[i].depth;
     
    // cout<<length<<": "<<articleInput[length].value<<"  "<<articleInput[length].depth<<endl;
    // cout<<length-1<<": "<<articleInput[length-1].value<<endl<<article[article[i].rightChild].value<<endl;
     articleInput[++length].value = article[i].value + article[article[i].leftChild].value + article[article[i].rightChild].value;
     articleInput[length].depth = article[i].value*article[i].depth+ article[article[i].leftChild].value*article[article[i].leftChild].depth + article[article[i].rightChild].value*article[article[i].rightChild].depth;
    
    // cout<<length<<": "<<articleInput[length].value<<"  "<<articleInput[length].depth<<endl;
    }
   }
   else
   {
   }
  }
 }

 

 
 for (i=1; i<=length; i++)
 {
  for (j=0; j<=totalMoney; j++)
  {
   //if (articleInput[i].value>j) max[i][j] = max[i-1][j];
   max[i][j]= max[i-1][j];
   

   if (j>=articleInput[i].value && max[i][j]<max[i-1][j-articleInput[i].value]+articleInput[i].depth)
   {
    max[i][j] = max[i-1][j-articleInput[i].value]+articleInput[i].depth;
   }
   {
   // max[i][j] = Big(max[i-1][j], max[i-1][j-articleInput[i].value]+articleInput[i].depth);
   }
   //cout<<max[i][j];
  }
 // cout<<i<<':'<<articleInput[i].depth<<' '<<max[i][totalMoney]<<endl;
 }//物品
 cout<<max[length][totalMoney];
 return 0;
}