So I just started exploring dynamic programming a little bit more in depth and I have come across a question which I've been unable to solve for a while now. I would greatly appreciate any help with it.
Find the maximum revenue for a salesman who knows how much revenue he will get in each city per day, given he is free for len(days_travel) days;
The input list revenue shows the revenue he will get on a particular day at a particular city (eg. revenue[0][1] = 2: day 0 and at city 'b') and travel days is the number of days he requires to travel from one city to the other (eg. days_travel[2][1] = 2 days is required to move from city c to city b and start is the city he starts in (at day 0)
the salesman can decide whether he will stay and sell in that city and earn revenue[day][city] or travel to another city from for 'x' number of days: So he will be on day + days_travel[start_city][to_city] day
Ex. (start = 1)
# city: a b c # day:
revenue = [[1, 2, 1], # 0
[3, 3, 1], # 1
[1, 1, 100]] # 2
# city: a b c # city:
days_travel = [[0, 1, 1], # a
[ 1, 0, 1], # b
[ 1, 2, 0]] # c
max revenue = 102
Path:
- Start at city 'b' on day 0, sell there, and earn 2
- Now at day 1, travel to city c in the morning
- Now at day 2, arrives at city C in the morning, work there and earn 100
My step:
Performed Floyd Warshall algorithm to days_travel, to find the shortest days from one city to the other, which using example above, results in the same matrix (but definitely can be different in others)
def floyd_warshall(days_travel):
nV = len(G)
# Adding vertices individually
for k in range(nV):
for i in range(nV):
for j in range(nV):
G[i][j] = min(G[i][j], G[i][k] + G[k][j])
which gives me ->
# city: a b c # city:
days_travel = [[0, 1, 1], # a
[ 1, 0, 1], # b
[ 1, 2, 0]] # c
I then thought about looping through everything and using max() but doesn't work
for x in range(len(days_travel)):
for y in range(len(days_travel)):
for day in range(len(revenue)):
I would appreciate some help with this. Thanks
from dynamic programming question similar to traveling salesman in python
No comments:
Post a Comment