[U]3.3.2 DP

來源:互聯網
上載者:User

赤裸裸的DP題,簡單題。

/*ID:bysenLANG:C++PROG:shopping*/#include<stdio.h>using namespace std;int ticket_No[100][6];int ticket_Num[100][6];int ticket_Money[100];//票的成員,成員的數量,優惠金額int hash[1000];//編號映射 int price[5];//商品價格int getGood[5];//商品的數量 int ticketNum;int goodNum;int min( int a,int b ){ return a<b?a:b; }void init(){  for( int i=0;i<1000;i++ ) hash[i]=-1;   int n;  goodNum=0;  scanf( "%d",&ticketNum );  for( int i=0;i<ticketNum;i++ )  {    scanf( "%d",&n );    for( int j=0;j<n;j++ )      scanf( "%d %d",&ticket_No[i][j],&ticket_Num[i][j] );//擷取優惠券上的產品編號與產品數量        scanf( "%d",&ticket_Money[i] ); }  for( int i=0;i<1000;i++ )   hash[i]=0x7FF;    scanf( "%d",&goodNum ); for( int i=0;i<goodNum;i++ ) {    int no,num,p;    scanf( "%d %d %d",&no,&num,&p );    hash[no]=i;    getGood[i]=num;    price[i]=p;     }}void DP(){  int a[5];  int dp[6][6][6][6][6];  for( a[0]=0;a[0]<=getGood[0];a[0]++ )  for( a[1]=0;a[1]<=getGood[1];a[1]++ )  for( a[2]=0;a[2]<=getGood[2];a[2]++ )  for( a[3]=0;a[3]<=getGood[3];a[3]++ )  for( a[4]=0;a[4]<=getGood[4];a[4]++ )  {    dp[a[0]][a[1]][a[2]][a[3]][a[4]]=a[0]*price[0]+a[1]*price[1]+a[2]*price[2]+a[3]*price[3]+a[4]*price[4];    if( a[0]-1>=0 )      dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[a[0]-1][a[1]][a[2]][a[3]][a[4]]+price[0],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );    if( a[1]-1>=0 )      dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[a[0]][a[1]-1][a[2]][a[3]][a[4]]+price[1],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );    if( a[2]-1>=0 )      dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[a[0]][a[1]][a[2]-1][a[3]][a[4]]+price[2],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );    if( a[3]-1>=0 )      dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[a[0]][a[1]][a[2]][a[3]-1][a[4]]+price[3],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );    if( a[4]-1>=0 )      dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[a[0]][a[1]][a[2]][a[3]][a[4]-1]+price[4],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );            for( int k=0;k<ticketNum;k++ )    {      bool flag=true;      int b[5];      for( int i=0;i<5;i++ )      b[i]=a[i];            for( int i=0;ticket_No[k][i]!=0;i++ )//遍曆優惠券      {  if( hash[ticket_No[k][i]]>goodNum )  flag=false;else if( b[hash[ticket_No[k][i]]]-ticket_Num[k][i]>=0 ){  b[hash[ticket_No[k][i]]]-=ticket_Num[k][i];  }  else  flag=false;   }    if( flag==false )      continue;       dp[a[0]][a[1]][a[2]][a[3]][a[4]]=min( dp[b[0]][b[1]][b[2]][b[3]][b[4]]+ticket_Money[k],dp[a[0]][a[1]][a[2]][a[3]][a[4]] );  }  //printf( "[%d][%d][%d][%d][%d]=%d\n",a[0],a[1],a[2],a[3],a[4],dp[a[0]][a[1]][a[2]][a[3]][a[4]] ); } printf( "%d\n",dp[getGood[0]][getGood[1]][getGood[2]][getGood[3]][getGood[4]] );}int main(){ freopen( "shopping.in","r",stdin ); freopen( "shopping.out","w",stdout ); init(); DP(); return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.