#P4367. [Code+#4] Tommy 的结合

[Code+#4] Tommy 的结合

Description

Life is probably like this. In this materialistic world, people hustle for a living, talk less and less, and grow more distant emotionally. However, the latest research by the renowned scientist Access Globe can solve this: increase the shared topics by increasing the things done simultaneously.

A and B each receive some tasks. Let the sets of tasks they need to execute be VAV_A and VBV_B, respectively. For each person, task 11 must be done first, and each other task ee has a prerequisite task pep_e, meaning task ee can be executed only after pep_e is completed. That is, the dependency of each person’s tasks forms a rooted directed tree whose edges point away from the root. A task ee can be executed if and only if all tasks pe,ppe,,1p_e, p_{p_e}, \cdots, 1 have been executed. We say task ee depends on tasks pe,ppe,,1p_e, p_{p_e}, \cdots, 1.

Now, A and B hope to complete some tasks together, so they decide to select tasks as follows: A selects mm tasks a1,,ama_1, \cdots, a_m with the requirements a1=1a_1 = 1 and, for any 1i<m1 \le i < m, task ai+1a_{i+1} depends on aia_i. Similarly, B selects mm tasks b1,,bmb_1, \cdots, b_m satisfying the same requirements. Then A can execute tasks along the path from 11 to ama_m in order, and B can execute tasks along the path from 11 to bmb_m in order. Moreover, after scheduling, when A is executing task aia_i, B is executing task bib_i at the same time. At such moments, A and B can get in touch and increase their intimacy.

The objective is to maximize the intimacy. For each pair of simultaneously executed tasks aia_i and bib_i, they gain a score of Cai,biC_{a_i, b_i}. Meanwhile, once A and B lose contact, their intimacy decreases: in each minute, if it is the ii-th minute since one side last contacted the other, the intimacy decreases by 2i12i - 1.

For example, suppose the times for the two people’s tasks are 2, 1, 4, 7 and 4, 8, 3, 6, 4, and they jointly complete only the first and the last tasks. Then A has 1 + 4 = 5 minutes without contact while executing the two middle tasks, which causes an intimacy decrease of 1+3++11=251 + 3 + \cdots + 11 = 25; B has 8 + 3 + 6 = 17 minutes without contact while executing the three middle tasks, which causes an intimacy decrease of 1+3++35=2891 + 3 + \cdots + 35 = 289. Note that the intimacy calculation depends only on task execution times, and any two tasks can be executed simultaneously as aia_i and bib_i; they need not take the same amount of time.

Given A’s and B’s tasks, dependencies, and the execution time of each task, please compute the maximum intimacy that can be achieved.

Input Format

Read from standard input.

  • The first line contains two integers VA,VB|V_A|, |V_B|.
  • The second line contains VA1|V_A| - 1 integers t2(a),t3(a),,tVA(a)t^{(a)}_{2}, t^{(a)}_{3}, \ldots, t^{(a)}_{|V_A|}, the duration of each of A’s tasks.
  • The third line contains VB1|V_B| - 1 integers t2(b),t3(b),,tVB(b)t^{(b)}_{2}, t^{(b)}_{3}, \ldots, t^{(b)}_{|V_B|}, the duration of each of B’s tasks.
  • The fourth line contains VA1|V_A| - 1 integers p2(a),p3(a),,pVA(a)p^{(a)}_{2}, p^{(a)}_{3}, \ldots, p^{(a)}_{|V_A|}, the prerequisite of each of A’s tasks, with pi(a)<ip^{(a)}_{i} < i guaranteed.
  • The fifth line contains VB1|V_B| - 1 integers p2(b),p3(b),,pVB(b)p^{(b)}_{2}, p^{(b)}_{3}, \ldots, p^{(b)}_{|V_B|}, the prerequisite of each of B’s tasks, with pi(b)<ip^{(b)}_{i} < i guaranteed.
  • Then follow VA1|V_A| - 1 lines, each with VB1|V_B| - 1 integers. In the (i1)(i - 1)-th row and (j1)(j - 1)-th column is Ci,jC_{i, j}, the intimacy gained if A and B simultaneously execute tasks ii and jj, respectively. Note that these intimacy scores are not necessarily nonnegative.

Note: the above input does not include information for task 11, because it is irrelevant to the intimacy calculation.

Output Format

Output to standard output.

Print the maximum achievable intimacy.

5 4
2 1 2 1
1 1 1
1 2 3 4
1 2 2
-8 -1 6
4 -3 7
-7 5 5
-7 5 -5
5

Hint

Sample explanation:

A and B select tasks 1,3,41, 3, 4 and 1,2,31, 2, 3, respectively. The simultaneous pairs (1,1)(1, 1), (3,2)(3, 2), and (4,3)(4, 3) yield intimacy 4+5=94 + 5 = 9. A executes task 22 alone for 2 minutes and loses 1+3=41 + 3 = 4 intimacy. Therefore, the final total intimacy is 94=59 - 4 = 5. This is the optimal plan.

0

Constraints: For all testdata, 2VA,VB2,6662 \le |V_A|, |V_B| \le 2{,}666, 1ti(a),ti(b)1,2061 \le t^{(a)}_i, t^{(b)}_i \le 1{,}206, and 0Ci,j2,017,011,3280 \le |C_{i, j}| \le 2{,}017{,}011{,}328.

Credit: https://www.luogu.org/discuss/show/38908

Credit: idea and problemsetting/Chen Junkun; problem verification/Tommy > <

Git Repo: https://git.thusaac.org/publish/CodePlus4

Thanks to Tencent for supporting this contest.

Translated by ChatGPT 5