new dp omptimization to avoid computing exponents of zero edges