algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

problem.txt (2434B)


      1 D. Kefa and Dishes
      2 time limit per test
      3 2 seconds
      4 memory limit per test
      5 256 megabytes
      6 
      7 When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.
      8 
      9 Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type — if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c.
     10 
     11 Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
     12 Input
     13 
     14 The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.
     15 
     16 The second line contains n space-separated numbers ai, (0 ≤ ai ≤ 109) — the satisfaction he gets from the i-th dish.
     17 
     18 Next k lines contain the rules. The i-th rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexes i and j (1 ≤ i < j ≤ k), that xi = xj and yi = yj.
     19 
     20 ---
     21 
     22 thoughts:
     23 
     24 - there are n dishes and we want m dishes
     25     - can't order same dish twice
     26 - dish i gives a_i satisfaction
     27 - food eating together rules
     28     - if he eats dish x exactly before dish y, then his satisfaction level raises by c
     29 
     30 ---
     31 
     32 ex1
     33 
     34 n,m,k
     35 
     36 n = 2 (list items)
     37 m = 2 (to order)
     38 k = 1 (triples)
     39 
     40 items := 1 1
     41 rules := [2 1 1]
     42 
     43 rules says if we eat 2 right before 1, we get 1 extra reward
     44 
     45 we want to do this:
     46 
     47 get 2, get 1
     48 
     49 1 satisfaction, 1 satisfaction + 1 addl bonus = 3
     50 
     51 ---
     52 
     53 n = 4
     54 m = 3
     55 k = 2
     56 
     57 satisfactions := 1 2 3 4
     58 
     59 [2 1 5]
     60 [3 4 2]
     61 
     62 1 after 2 and 4 after 3 get bonuses
     63 
     64 order 2 (2) 
     65 order 1 (6)
     66 order 4 (4)
     67 
     68 = 12 (better)
     69 
     70 order 3 (3)
     71 order 4 (6)
     72 order 2 (2)
     73 
     74 = 11
     75 
     76 ---
     77 
     78 best selections:
     79 
     80 - max score after the current state given our prior state
     81 - dp with state of (selected, prior)