背包

01背包。输出方案。

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stack>
using namespace std;
int v[1000], num[1000], Vp[1000];
const int inf = 0x7f7f7f7f;
int dp[1100][1100];
int st[1110], top;

//输出方案种类 
void DFS( int x, int volume, int sum, int cnt)
{
  
  if( sum == 1000 )
  {
      while( sum && cnt >= 1)
      {
          printf("%d ", st[cnt]);
          cnt--;
          sum -= st[cnt];       
      }
      puts("");    
  }
  if( x == 0 && volume == 0)
      return;
  if( x != 0 )
  {
     if( dp[x-1][volume] > 0 && x - 1 >= 0)
         DFS(x-1, volume, sum, cnt);
     if( dp[x][volume - v[x]] > 0 && volume - v[x] >= 0)
     {
         st[++cnt] = v[x];
         DFS(x, volume - v[x], sum + v[x], cnt);    
     }    
  }        
}

int main( )
{
  int N;
  while( scanf("%d",&N) != EOF)
  {
    for( int i = 1; i <= N; i++)
    {
      scanf("%d",&v[i]);
    }
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for( int i = 1; i <= N; i++)
    {
       for( int j = 0; j <= 1000; j++)
       {
          if( j >= v[i] )
          dp[i][j] = dp[i-1][j] + dp[i-1][j - v[i]];
          else
          dp[i][j] = dp[i-1][j];
       }
    } 
    printf("总的方案数:\n"); 
    printf("%d\n", dp[N][1000]);
    puts("各种方案"); 
    DFS(N, 1000, 0, 0);
  }    
  return 0;  
}

01背包,求方案数

View Code
//求01背包方案数 
#include<stdio.h>
#include<string.h>
int dp[1100], v[1100];
int main( )
{
  int N;
  while( scanf("%d",&N) != EOF)
  {
     for( int i = 1; i <= N; i++)
         scanf("%d",&v[i]);
     memset(dp, 0,sizeof(dp));
     dp[0] = 1;
     for( int i = 1; i <= N; i++)
       for( int V = 1000; V >= v[i]; V--)
            dp[V] = dp[V] + dp[V - v[i]];
     printf("%d\n",dp[1000]);
  }
}

01背包。滚动数组 Bone collector

View Code
/*
0-1背包
*/ 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int v[1100], m[1100];
int dp[2][1100];//232k dp[1100][1100], 5000k

int max( int x, int y)
{
  return x > y ? x : y;   
}

int main( )
{
  int N, M, T;
  while( scanf("%d",&T) != EOF)
  {   
     while( T-- )
     {
      scanf("%d%d", &N, &M);
      for( int i = 1; i <= N; i++)
           scanf("%d",&m[i]);
      for( int i = 1; i <= N; i++)
           scanf("%d",&v[i]);
      memset(dp, 0, sizeof(dp));
      for( int i = 1; i <= N; i++)
      {
           for( int j = 0; j <= M; j++)
           {
              if( j >= v[i] )
              {
                dp[i % 2][j] = max(dp[(i-1) % 2][j], dp[(i-1)%2][j - v[i]] + m[i]);  
              }
              else
                dp[i % 2][j] = dp[(i-1) % 2][j];
                
           }
      }
      printf("%d\n", dp[N % 2][M]);
    }       
  }
  return 0;   
}

01背包

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
using namespace std;
int w[2100];
int dp[2010][2010]; ////从n件物品中选K件
const int inf = 0x7f7f7f7f;


int main( )
{
   int N, K;
   while( scanf("%d%d",&N, &K) != EOF)
   {
      for( int i = 1; i <= N; i++)
         scanf("%d",&w[i]);
      sort(w + 1, w + N + 1);
      for( int i = 0; i <= N; i++)
      {      
         dp[i][0] = 0;
         for( int j = 1; j <= N; j++)
             dp[i][j] = inf;
      }
      for( int i = 1; i <= N; i++)
      {
              for( int k = 1; k <= i; k++)
              {
               int val = (w[i] - w[i-1]) * (w[i] - w[i-1]);
               dp[i][k] = min(dp[i][k], dp[i-2][k-2] + val);//);//这个物品选了
               dp[i][k] = min(dp[i][k], dp[i-1][k]); //没选 
              }
      }
      printf("%d\n",dp[N][2 * K]);
   }
   return 0; 
}

HDU 1712

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int cost[110][110];
int dp[1000];

int max( int x, int y)
{
 return x > y ? x : y;
}

int main( )
{
  int N, M;
  while( scanf("%d%d", &N, &M) != EOF)
  {
     for( int i = 1; i <= N; i++)
        for( int j = 1; j <= M; j++)
        scanf("%d",&cost[i][j]);
     memset(dp, 0, sizeof(dp));
     for( int i = 1; i <= N; i++)
       for(int v = M; v >= 0; v--)
         for( int k = 1; k <= M; k++)
            if( v >= k )
            dp[v] = max(dp[v], dp[v - k] + cost[i][k] );    
     printf("%d\n", dp[M]);
  }
  return 0;
}

 

posted on 2012-08-10 13:47  more think, more gains  阅读(174)  评论(0编辑  收藏  举报

导航