Giter Club home page Giter Club logo

pack's Introduction

pack

背包问题九讲 #include using namespace std;

const int N = 3;//物品个数 const int V = 5;//背包最大容量 int weight[N + 1] = {0,3,2,2};//物品重量 int value[N + 1] = {0,5,10,20};//物品价值

int f[N + 1][V + 1] = {{0}};

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

/* 目标:在不超过背包容量的情况下,最多能获得多少价值

子问题状态:f[i][j]:表示前i件物品放入容量为j的背包得到的最大价值

状态转移方程:f[i][j] = max{f[i - 1][j],f[i - 1][j - weight[i]] + value[i]}

初始化:f数组全设置为0 */ int Knapsack() { //初始化 memset(f,0,sizeof(f)); //递推 for (int i = 1;i <= N;i++) //枚举物品 { for (int j = 0;j <= V;j++) //枚举背包容量 { f[i][j] = f[i - 1][j]; if (j >= weight[i]) { f[i][j] = Max(f[i - 1][j],f[i - 1][j - weight[i]] + value[i]); } } } return f[N][V]; }

int main() { cout<<Knapsack()<<endl; system("pause"); return 1; }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.