Created
March 15, 2015 20:48
-
-
Save anupamkumar/eea9570614a8a8837565 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def minCost(cost,N): | |
best_way_to_reach = [0]*N | |
for k in range(1,N): | |
minimum_cost_to_get_from_0_to_k = sys.maxint | |
for i in range(k,0,-1): | |
if(minimum_cost_to_get_from_0_to_k > best_way_to_reach[k-i]+cost[k-i][k]): | |
minimum_cost_to_get_from_0_to_k = best_way_to_reach[k-i]+cost[k-i][k] | |
best_way_to_reach[k] = min(minimum_cost_to_get_from_0_to_k,cost[0][k]) | |
return best_way_to_reach[N-1] | |
mycost = [[0,15,80,90],[sys.maxint,0,40,50],[sys.maxint,sys.maxint,0,70],[sys.maxint,sys.maxint,sys.maxint,0]] | |
N = 4 | |
print minCost(mycost,N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment