#P2465. [SDOI2008] 山贼集团

    ID: 1471 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2008各省省选山东O2优化枚举,暴力状态压缩,状压

[SDOI2008] 山贼集团

Description

A bandit syndicate holds great power in Green Shade Village. The whole Green Shade Village consists of nn connected hamlets, and it is guaranteed that for any two hamlets there is exactly one simple path between them. The hamlets are numbered from 11 to nn, and the syndicate’s headquarters is located in hamlet 11.

Besides the boss stationed at the headquarters, the other pp departments want to establish branches elsewhere in the village (Luogu note: this actually also includes the headquarters). The pp branches can be built in the same hamlet or in different hamlets, and the cost of building different departments in different hamlets varies.

The path from each branch to the headquarters is defined as that department’s coverage. Therefore, the coverages of these pp branches may overlap or even be exactly the same. When multiple branches cover the same hamlet, conflicts may occur and cause some loss of profit, or cooperation may occur and generate some profit.

Please note that if the same set of branches simultaneously covers multiple hamlets, then the profit/loss for that set is counted once for each such hamlet.

Now you are to write a program to determine the locations of the pp branches so that the syndicate’s profit is maximized.

Input Format

  • Line 1: Two integers nn and pp, the number of hamlets and the number of departments.
  • Lines 22 to nn: Each line contains two integers x,yx, y, indicating a road between hamlet xx and hamlet yy.
  • Lines (n+1)(n + 1) to 2n2n: Each line contains pp integers. On line (i+n)(i + n), the jj-th integer is the cost ai,ja_{i, j} of building department jj in hamlet ii.
  • Line (2n+1)(2n + 1): An integer tt, the number of inter-department influence items.
  • Lines (2n+2)(2n + 2) to (2n+t+1)(2n + t + 1): Each line starts with an integer vv; if vv is positive, extra profit is gained, and if vv is negative, a loss occurs. Then an integer cc, the number of involved departments. Then cc integers, the involved departments xix_i. The meaning is: if these cc departments simultaneously cover some hamlet (possibly along with other departments also covering that hamlet), then an extra profit or loss of v|v| is applied for each such hamlet.

Output Format

Output a single integer, the maximum achievable profit.

2 1
1 2
2 
1
1
3 1 1
5

Hint

Explanation for Sample 1:

If department 11 is built at node 22, the cost is 11. Then the set of branches {1}\{1\} covers nodes 11 and 22. According to the first influence item, this set yields a profit of 33 per covered node, so the total profit is 2×3=62 \times 3 = 6. Subtracting the building cost, the maximum profit is 61=56 - 1 = 5. It can be proven that no better plan exists.

Constraints:

  • For 40%40\% of the testdata, 1p61 \leq p \leq 6.
  • For 100%100\% of the testdata, it is guaranteed that:
    • 1p121 \leq p \leq 12, 1n1001 \leq n \leq 100.
    • 1ai,j1081 \leq a_{i, j} \leq 10^8.
    • 1t2p1 \leq t \leq 2^p, 1v1081 \leq |v| \leq 10^8, 1cp1 \leq c \leq p, 1xip1 \leq x_i \leq p, and the xix_i are pairwise distinct.
    • The absolute value of the answer does not exceed 10810^8.

Translated by ChatGPT 5