Advertisement
Kamend1

Chain of LIghtning Kamen Dimitrov

Jul 30th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. from collections import deque
  2.  
  3. class Edge:
  4. def __init__(self, destination, distance):
  5. self.destination = destination
  6. self.distance = distance
  7.  
  8. def bfs(graph, start_node, damage, total_damage):
  9. queue = deque([(start_node, damage)])
  10. visited = set([start_node])
  11.  
  12. while queue:
  13. current_node, current_damage = queue.popleft()
  14.  
  15. # Update the total damage for the current node
  16. total_damage[current_node] += current_damage
  17.  
  18. for edge in graph[current_node]:
  19. neighbor = edge.destination
  20. if neighbor not in visited:
  21. new_damage = current_damage // 2
  22. queue.append((neighbor, new_damage))
  23. visited.add(neighbor)
  24.  
  25. nodes = int(input())
  26. edges = int(input())
  27. strikes = int(input())
  28.  
  29. graph = [[] for _ in range(nodes)]
  30.  
  31. for i in range(edges):
  32. source, destination, distance = map(int, input().split())
  33. graph[source].append(Edge(destination, distance))
  34. graph[destination].append(Edge(source, distance)) # Add for both nodes since it's an undirected graph
  35.  
  36. total_damage = [0] * nodes
  37. visited = set()
  38.  
  39. for _ in range(strikes):
  40. start_node, damage = map(int, input().split())
  41. if start_node not in visited:
  42. bfs(graph, start_node, damage, total_damage)
  43.  
  44. highest_damage = max(total_damage)
  45. print(highest_damage)
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement