LouisCK's blog

By LouisCK, 7 years ago, In English

I am trying to solve this problem called SCUBADIV on SPOJ. I am getting the NZEC error repeatedly. Could someone help me and tell me which part of my code is causing the error?

T = int(raw_input())

class Cylinder(object):
    def __init__(self, a, b, c):
        self.oxygen = a
        self.nitrogen = b
        self.weight = c
    def get_ox(self): return self.oxygen
    def get_nit(self): return self.nitrogen
    def get_weight(self): return self.weight

def solver(cylinders, pos, oxygen, nitrogen, map):
    if pos == len(cylinders) - 1:
        if oxygen - cylinders[pos].get_ox() <= 0 and nitrogen - cylinders[pos].get_nit() <= 0: return cylinders[pos].get_weight()
        else: return float('inf')
        if (pos, oxygen, nitrogen) in map: return map[(pos, oxygen, nitrogen)]
            #don't take the item
            b = solver(cylinders, pos + 1, oxygen, nitrogen, map)
            #take the item
            if oxygen - cylinders[pos].get_ox() <= 0 and nitrogen - cylinders[pos].get_nit() <= 0: a = cylinders[pos].get_weight()
            else: a = cylinders[pos].get_weight() + solver(cylinders, pos + 1, oxygen - cylinders[pos].get_ox(), nitrogen - cylinders[pos].get_nit(), map)
            map[(pos, oxygen, nitrogen)] = min(a, b)
            return min(a, b)
for i in range(T):
    t = raw_input().split()
    o, n = int(t[0]), int(t[1])
    k = int(raw_input())
    cylinders = []
    for j in range(k):
        inp = raw_input().split()
        t, a, w = int(inp[0]), int(inp[1]), int(inp[2])
        cylinders.append(Cylinder(t, a, w))
    print solver(cylinders, 0, o, n, {})

  • Vote: I like it
  • 0
  • Vote: I do not like it