-
Notifications
You must be signed in to change notification settings - Fork 6
/
01背包.py
executable file
·34 lines (26 loc) · 1.01 KB
/
01背包.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/10/9 1:21 PM
# @Author : Slade
# @File : 01背包.py
import numpy as np
def dp(weight, count, weights, costs):
goods = [[0 for _ in range(weight + 1)] for _ in range(count + 1)]
for i in range(1, count + 1):
for j in range(1, weight + 1):
goods[i][j] = goods[i - 1][j]
if weights[i - 1] <= j and goods[i][j] < goods[i - 1][j - weights[i - 1]] + costs[i - 1]:
goods[i][j] = goods[i - 1][j - weights[i - 1]] + costs[i - 1]
return goods
def bags(w,c,ws,v):
dp=[[0 for i in range(w+1)] for j in range(c+1)]
for i in range(1,c+1):
for j in range(1,w+1):
if ws[i-1]>j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = max(dp[i-1][j-ws[i-1]]+v[i-1],dp[i-1][j])
return dp
if __name__ == '__main__':
print(np.array(bags(12, 6, [4, 6, 2, 2, 5, 1], [8, 10, 6, 3, 7, 2])))
print(np.array(dp(12, 6, [4, 6, 2, 2, 5, 1], [8, 10, 6, 3, 7, 2])))