01背包问题python实现
来源:互联网 发布:淘宝名字叫什么好 编辑:程序博客网 时间:2024/06/05 12:04
可以记录路径,有时间再完善
# -*- coding:utf-8 -*-def bag(n, c, w, v): res = [[0 for j in range(c + 1)] for i in range(n + 1)] for j in range(c + 1): res[0][j] = 0 for i in range(1, n + 1): for j in range(1, c + 1): res[i][j] = res[i - 1][j] if j >= w[i - 1] and res[i][j] < res[i - 1][j - w[i - 1]] + v[i - 1]: res[i][j] = res[i - 1][j - w[i - 1]] + v[i - 1] print res return resdef show(n, c, w, res): print '最大价值为:', res[n][c] x = [False for i in range(n)] j = c i = n while i > 0: if res[i][j] > res[i - 1][j]: x[i - 1] = True j -= w[i - 1] i -= 1 else: i -= 1 print '选择的物品为:' for i in range(n): if x[i]: print '第', i, '个'if __name__ == '__main__': n = 5 c = 17 w = [2, 2, 6, 5, 4] v = [2, 2, 6, 5, 4] res = bag(n, c, w, v) show(n, c, w, res)
0 0
- 01背包问题python实现
- 01背包问题(动态规划)python实现
- 01背包问题(回溯法)python实现
- 动态规划之背包问题 python实现
- 01背包、完全背包、多重背包问题的C++实现
- 01背包问题(java实现)
- 01背包问题-基本实现
- 01背包问题Java实现
- 01背包问题 java实现
- 01背包问题与分数背包问题 C++实现
- 01背包问题 (python版)
- 背包问题(c/python)
- 0/1背包问题算法的python实现
- 分支限界法的0-1背包问题Python实现
- 背包问题---01背包|完全背包(装满背包的方案总数分析及实现)
- 背包问题---01背包
- 背包问题---01背包(原理,伪代码,编程实现)
- 01背包问题的java界面实现
- AIDL解析
- Python Numpy Tutorials: 数组--4
- Http协议相关知识
- WP_使用_FastCGI_Cache_实现高效页面缓存
- ReactiveCocoa学习篇——【上】
- 01背包问题python实现
- CentOS7下内核源码下载及编译步骤
- 如何避免SSH Secure Shell Client连接Linux超时,自动断开?
- 编程实现strcpy和memcpy函数
- android经常性错误解决
- python if判断
- 或许是_2017年_最新的_ngx_pagespeed_安装姿势
- 第三天 H5进阶
- 细粒度图像识别文章 Picking Deep Filter Responses for Fine-grained Image Recognition 阅读笔记