Предмет: Информатика, автор: kurochkaandrew624

Помогите найти ошибку
from collections import defaultdict

def find_max_happiness_tour(V, E, p_values, routes):
graph = defaultdict(list)

for route in routes:
A, B = route[0], route[1]
graph[A].append((B, p_values[B-1]))
graph[B].append((A, p_values[A-1]))

max_happiness = 0

for start_city in range(1, V+1):
visited = set()
current_city = start_city
current_happiness = 0

for _ in range(4): # At most four days
visited.add(current_city)
max_neighbor_happiness = 0
next_city = None

for neighbor, happiness in graph[current_city]:
if neighbor not in visited and happiness > max_neighbor_happiness:
max_neighbor_happiness = happiness
next_city = neighbor

if next_city is not None:
current_city = next_city
current_happiness += max_neighbor_happiness

max_happiness = max(max_happiness, current_happiness)

return max_happiness


V = 5
E = 6
p_values = [3, 2, 4, 1, 5]
routes = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3)]

result = find_max_happiness_tour(V, E, p_values, routes)
print(result)
помогите, срочно надо

Приложения:

Ответы

Автор ответа: ggig77895
1

Ответ:

у вас отсутствует return-инструкция после внутреннего цикла, что может привести к неправильным результатам. Вот исправленный вариант кода:

from collections import defaultdict

def find_max_happiness_tour(V, E, p_values, routes):

   graph = defaultdict(list)

   for route in routes:

       A, B = route[0], route[1]

       graph[A].append((B, p_values[B-1]))

       graph[B].append((A, p_values[A-1]))

   max_happiness = 0

   for start_city in range(1, V+1):

       visited = set()

       current_city = start_city

       current_happiness = 0

       for _ in range(4):  # At most four days

           visited.add(current_city)

           max_neighbor_happiness = 0

           next_city = None

           for neighbor, happiness in graph[current_city]:

               if neighbor not in visited and happiness > max_neighbor_happiness:

                   max_neighbor_happiness = happiness

                   next_city = neighbor

           if next_city is not None:

               current_city = next_city

               current_happiness += max_neighbor_happiness

       max_happiness = max(max_happiness, current_happiness)

   return max_happiness

V = 5

E = 6

p_values = [3, 2, 4, 1, 5]

routes = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3)]

result = find_max_happiness_tour(V, E, p_values, routes)

print(result)

Похожие вопросы
Предмет: Оʻzbek tili, автор: jurayevamohichehra91
Предмет: Английский язык, автор: levmironchuk
My grandma Yvonne (go) to a concert in Newcastle in 1969. The venue was City Hall and the star was Gene Pitney. Grandma (love) Gene Pitney. She (have) many posters of him on her bedroom wall. It was her first concert. The ticket was expensive, but grandma (not care). She (arrive) at the concert in good time. They (sit) down. They were excited! When Gene Pitney (stand) up and (go) with her best friend, Lucy. They (get) straight to their seats and (come) on stage, everyone (make) people sit down. Grandma (can) not see the stage. As soon as (cheer). Happily, the bouncers was pleased because she is quite short and she Gene Pitney (begin) singing, fans started to throw jelly babies at him. It was a custom in England at the time. It (not know) this. He they were rude. He (show) that people were happy. Gene Pitney, an American, (think) the crowd did not like him. He (refuse) to sing. Then he My grandma was very upset. Eventually, the star He only (think) (shout) insults at everybody. (walk) off and (sing) two songs. What a terrible night! By Flora aged 14 (not return).
Say if the following sentences are true or false. Correct the false sentences:
1. The concert took place at City Hall, Paris. F- Newcastle.
2. Yvonne went to see Gene Pitney.
3. The ticket was cheap.
4. She went with her boyfriend.
5. When the star came on, all the fans stood up.
6. Fans threw bottles.
7. Gene Pitney was English.
8. The star thanked the fans kindly.
9. He sang many songs.
10. He left the stage

ответить на опросы True\False