Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import re
  2. import numpy as np
  3. from tqdm import tqdm
  4. def read_instance(filename):
  5.     f=open(filename)
  6.     prices=[]
  7.     weights=[]
  8.     n=int(next(f).strip())
  9.     for line in f:
  10.         line=[int(l)for l in re.split("[^\d]+",line.strip())]
  11.         if len(line)==3:
  12.             _,price,weight = line
  13.             prices.append(price)
  14.             weights.append(weight)
  15.         else:
  16.             w,=line
  17.     f.close()
  18.     return prices,weights,w
  19. valores,pesos,w=read_instance("../entradas/input12.in")
  20.  
  21. # print("uint w=%i,n=%i;"%(w,len(pesos)))
  22. # print("uint pesos[%i]=%s;"%(len(pesos),repr(pesos).replace("[","{").replace("]","}")))
  23. # print("uint valores[%i]=%s;"%(len(valores),repr(valores).replace("[","{").replace("]","}")))
  24.  
  25. # print(w*len(pesos)*4)
  26. n=len(pesos)
  27. mem=np.zeros((n+1)*(w+1),dtype=np.uint32).reshape((n+1,w+1))
  28.  
  29. def mochiladin(w , n):
  30.     for i in tqdm(range(n+1)):
  31.         for j in range(w+1):
  32.             if (i==0) or (j==0):
  33.                 mem[i][j] = 0;
  34.             elif (pesos[i-1] <= j):
  35.                 mem[i][j] = max(valores[i-1] + mem[i-1][j-pesos[i-1]],  mem[i-1][j]);
  36.             else:
  37.                 mem[i][j] = mem[i-1][j];
  38.     return mem[n][w];
  39. print(mochiladin(w,len(pesos)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement