码迷,mamicode.com
首页 > 编程语言 > 详细

C++ P1306 背包问题

时间:2016-11-15 20:01:02      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:namespace   run   space   scanf   cst   pen   ++   name   for   

#include<iostream>
#include<cstdio>
using namespace std;

int N,v[35],p[35],C,MAX;

void run(int x,int V,int P)//物品x放不放,目前为止的体积V,背包价值P;
{
  if(V>C) return;
  if(x>N){if(P>MAX) MAX=P;return;}
  for(int i=1;i>=0;i--)
    run(x+1,V+v[x*i],P+p[x*i]);
}

int main()
{
  freopen("test.in","r",stdin);
  freopen("test.out","w",stdout);
  scanf("%d%d",&C,&N);
  for(int i=1;i<=N;i++)
    scanf("%d%d",&v[i],&p[i]);
  run(1,0,0);
  printf("%d",MAX);
  return 0;
}

C++ P1306 背包问题

标签:namespace   run   space   scanf   cst   pen   ++   name   for   

原文地址:http://www.cnblogs.com/Bear-Child/p/6066670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!