Solving the 2010 ACM Problem: Locks

Post your best solutions! You can find a full description of the problem and examples here: ACM 2010 Problems (pdf)

You have a set of castles connected by roads, and you want to defeat all castles with a minimum number of soldiers. Each castle has three properties: the minimum number of soldiers needed to accept it, the number of soldiers who die taking it, and the number of soldiers that must be left to hold it.

There is exactly one path between any two castles (roads form a tree). You can choose any castle as your first target, but after that you must follow the roads. You can only travel on the road twice. Your mobile army should remain in one group.

+3
source share
3 answers

The first thing to understand is that as far as the numbers go, there is no difference between the lost soldiers and the remaining soldiers. Thus, we can reduce the properties of the castle to soldiers lost and wanted.

The second thing you need to understand is that if you go down a tree branch, you must fill out the entire branch to return. This allows us to reduce the entire branch to a single "mega-castle" with the required and lost total soldiers.

, , , : , . , , , . . , . ! , .

, , / , . 0 , , , . : , , . , (un) ( , ). , ( , ). : (un) , , . ( ) .

, :

  • (memoize , )
  • ( ).

O (n * c ^ 2 * lg (c)), n - , c - . , nc '', node clg (c) , . [ ]

, , , .

0

:

Bruteforce (100 ) :

[i], [i] , , , i, , , [i] [i] .

min_solder_to_attack_castle [i] .

, [] cost [] "" . , [] [] "", :

cost [i] = ( [childs])

[i] - : - max (min_solder_to_attack_castle [ ]) max (min_solder_to_attack_castle [ ]) + max ( [ ]). (number_of_childs)! , , n!, , , , .

+1

- , "" , , . , , , , "" .

, , " " , - .

, .

0
source

Source: https://habr.com/ru/post/1733297/


All Articles