'Calculate shortest path in graph with weighted Vertices
I am currently working on a kattis problem: Treasure Hunt, Link. The goal is to find the path which will cost the least amount of days to reach the treasure.
I currently use Dijkstra's algorithm with weighted vertexes to calculate the shortest route to the treasure. I have defined a class 'Node' in which I define it's weight and the previous Node if assigned. I use a heapq and needed to override the lt method in my Node class.
When the shortest route is defined I try to calculate the number of days that it will take to complete this shortest route.
I know it is not the fines code, I am sorry.
Defining neighbor nodes and the pathfinding
def createNode(row):
rl = list()
for x in row:
rl.append(Node(x))
return rl
rows, columns, stamina = map(int, input().split(' '))
treasure_map = list()
for row in range(rows):
treasure_map.append(list(input()))
treasure_map = list(map(createNode, treasure_map))
for x in range(len(treasure_map)):
for y in range(len(treasure_map[x])):
tile = treasure_map[x][y]
# add tile to south link
if x - 1 >= 0 and treasure_map[x - 1][y] is not None:
tile.add_neighbour(treasure_map[x - 1][y])
if y + 1 < len(treasure_map[x]) and treasure_map[x][y + 1] is not None:
tile.add_neighbour(treasure_map[x][y + 1])
if x+1 < len(treasure_map) and treasure_map[x + 1][y] is not None:
tile.add_neighbour(treasure_map[x + 1][y])
if y - 1 >= 0 and treasure_map[x][y - 1] is not None:
tile.add_neighbour(treasure_map[x][y - 1])
visited = list()
nodes = list()
for x in treasure_map:
for y in x:
if y.name is not '#':
nodes.append(y)
heapq.heapify(nodes)
endpoint = None
if len(nodes) < 2:
print(-1)
# Search route with minimum load
while nodes:
curr = heapq.heappop(nodes)
if curr.name is 'G':
endpoint = curr
break
for node in curr.get_neighbours():
if node not in visited and not node.load > stamina:
if curr.weight + curr.load < node.weight:
node.add_previous(curr)
visited.append(curr)
heapq.heapify(nodes)
The code of calculating the days it will take to get from the start to end: (once again I know it can be better)
if endpoint is not None:
nexNode = endpoint.previous
found = False
stamina_to_use = list()
while nexNode:
if nexNode.name == "S":
# Maybe count the day here
found = True
stamina_to_use.append(nexNode.load)
nexNode = nexNode.previous
days = 1
curr = stamina
counter = 0
stamina_to_use.reverse()
# Count the number of days needed for finishing
while stamina_to_use:
tocheck = stamina_to_use.pop()
# print("Current days: {}, current stamina: {},stamina to withdraw {}".format(
# days, curr, tocheck))
if curr > stamina:
print(-1)
break
if (curr - tocheck) < 0:
days += 1
curr = stamina
curr -= tocheck
if found:
print(days)
else:
print(-1)
else:
print(-1)
The results are actually as I expected, I get the shortest path and get the right number of days according to my own test cases and also to the ones on kattis. But for some reason when I submit the project to kattis the first 8 or so test cases pass and then I suddenly get: "Wrong answer", I don't know where the mistake in my thinking or code is. Is my approach the right one or should I use a different one. Or is there just a simple mistake made in counting the days?
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
