Some time ago I found the answer right there in the B-Prolog manual: "Note that, if table modes are not respected, or if there is no bound for an optimized argument, a program may give unexpected answers".
Code in the question doesn't respect the "table cost(+, min)." modes and tries to use first cost/2 parameter as an output.