luogu#P4775. [NOI2018] 情报中心

    ID: 3767 远端评测题 8000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018线段树NOIO2优化最近公共祖先 LCA

[NOI2018] 情报中心

Problem Description

In recent years, Country C and Country D have been at war.

Recently, Country C successfully infiltrated a city in Country D. This city can be modeled as an undirected graph with nn nodes, connected by n1n - 1 bidirectional edges, such that any two nodes are reachable from each other. In other words, this undirected graph is actually a tree.

After investigation, the minister of intelligence of Country C, GGB, was surprised to find that this seemingly ordinary city is actually the military center of Country D. Therefore, GGB decided to set up intelligence agencies in this city. After reconnaissance, the intelligence expert TAC prepared mm possible plans to set up intelligence agencies. In the ii-th plan, intelligence personnel are deployed on all edges along the shortest path from node xix_i to node yiy_i to collect intelligence, and this plan costs viv_i yuan.

However, due to lack of manpower, GGB can only implement two of the above mm plans. Meanwhile, TAC pointed out that, for these two intelligence agencies to cooperate better, the ranges of intelligence they collect must share at least one common edge. To evaluate a plan, GGB and TAC surveyed all edges and assigned each edge an intelligence value cic_i, meaning that collecting intelligence on this edge brings a profit of cic_i yuan. Note that intelligence is unique, so even if an edge’s intelligence is collected by both agencies, the profit is still only cic_i.

Now, please help GGB choose two legal plans to implement, such that the ranges of intelligence collected by these two plans share at least one common edge, and on this basis, the difference between total profit and total cost is maximized.

Note that this value may be negative, but it is still valid. If no such two plans can be found, output F.

Input Format

Read input from file center.in.

This problem contains multiple test cases.

The first line of the input file contains an integer TT, indicating the number of test cases.

Each test case contains (n+m+1)(n + m + 1) lines:

Line 11 contains an integer nn, the number of nodes in the city.

Lines 22 to nn (i.e., line (i+1)(i + 1)) each contain three integers aia_i, bib_i, cic_i, describing a bidirectional edge connecting nodes aia_i and bib_i with intelligence value cic_i. It is guaranteed that ai<bia_i < b_i and all bib_i are distinct.

Line (n+1)(n + 1) contains an integer mm, the number of plans proposed by TAC.

Lines (n+2)(n + 2) to (n+m+1)(n + m + 1) (i.e., line (n+i+1)(n + i + 1)) each contain three integers xix_i, yiy_i, viv_i, meaning that the ii-th plan deploys intelligence personnel on all edges along the shortest path from node xix_i to node yiy_i, and costs viv_i yuan.

Output Format

Write output to file center.out.

The output file contains TT lines.

For each test case, output one line: if there exists a legal choice, output an integer representing the maximum value of (total profit minus total cost); otherwise output F.

2
5
1 2 1
2 3 3
3 4 2
1 5 8
2
1 4 5
3 5 8
5
1 2 1
2 3 3
3 4 3
1 5 9
2
1 5 5
2 3 8
1
F
1
11
1 2 2
1 3 0
2 4 1
3 5 7
1 6 0
1 7 1
1 8 1
6 9 3
4 10 2
4 11 8
10
7 10 2
10 7 0
2 11 1
8 6 7
7 7 0
10 1 1
8 2 1
7 8 3
7 7 3
3 9 9

13

Hint

Sample 1 Explanation

This sample contains two test cases. The cities (trees) are the same in both cases; only the intelligence values and the plans differ. The city map is as follows:

  • For the first test case, the shortest path from node 11 to node 44 in plan 1 is 12341 \rightarrow 2 \rightarrow 3 \rightarrow 4, and the shortest path from node 33 to node 55 in plan 2 is 32153 \rightarrow 2 \rightarrow 1 \rightarrow 5. Choosing these two plans costs 5+8=135 + 8 = 13, and intelligence on every edge is collected, yielding a profit of 1+3+2+8=141 + 3 + 2 + 8 = 14, so (total profit minus total cost) is 1413=114 − 13 = 1.
  • For the second test case, the shortest path from node 11 to node 55 in plan 1 is 151 \rightarrow 5, and the shortest path from node 22 to node 33 in plan 2 is 232 \rightarrow 3. The ranges of intelligence collected by these two plans have no common edge, so this is illegal. Therefore, there is no legal choice for this test case, and you should output F.

Sample 2 Explanation

See the attached files center2.in and center2.ans.

This sample contains only one test case. In this test case, the optimal choice is to select plan 22 and plan 33.

The city map for this test case is as follows. The bold edges are those whose intelligence is collected by the intelligence centers; red edges are collected only by the intelligence center in plan 22; blue edges are collected only by the intelligence center in plan 33; and purple edges are collected by both intelligence centers.

Sample 3

See the attached files center3.in and center3.ans.

This sample has the same properties as test point 44. The properties of each test point are shown in the table below.

Sample 4

See the attached files center4.in and center4.ans.

This sample is undoubtedly a generous gift from the kind problem setter. It contains a large number of carefully constructed test cases with n100,m200n\le 100,m\le 200, covering all combinations of properties that appear among the test points. You can use this test point to thoroughly check your program. With enough test cases, small constraints, and diverse data types, bugs in your program will have nowhere to hide. The problem setter believes that this wonderful sample can provide strong support on your journey toward AC on this problem.

Constraints

The sizes and properties of the test points are shown in the table below:

::cute-table{tuack}

Test Point nn \le mm \le T50T \le 50 Special Property
1 22 33 Guaranteed None
2 1010 3030 ^ ^
3 200200 300300
4 10310^3 2,0002,000 ai=bi1a_i = b_i - 1
5 10410^4 3×1043 \times 10^4 ^
6 5×1045 \times 10^4 10510^5
7 10410^4 3×1043 \times 10^4 ci=0c_i=0
8 5×1045 \times 10^4 10510^5 ^
9 ^
10 10410^4 nn S1S_1
11 5×1045 \times 10^4 ^ Not guaranteed ^
12 ^ ^
13 10410^4 3×1043 \times 10^4 Guaranteed S2S_2
14 ^ ^
15 5×1045 \times 10^4 10510^5 Not guaranteed
16 ^
17 10410^4 3×1043 \times 10^4 Guaranteed None
18 5×1045 \times 10^4 105 10^5 ^ ^
19 ^ Not guaranteed
20 ^

The special properties in the table are:

  • Special property S1S_1: For any i,ji, j, it is guaranteed that the smallest-indexed node on the shortest path from xix_i to yiy_i is different from the smallest-indexed node on the shortest path from xjx_j to yjy_j.

  • Special property S2S_2: For any ii, it is guaranteed that the smallest-indexed node on the shortest path from xix_i to yiy_i is node 11.

For all testdata, 1n5×1041 \le n \le 5 \times 10^4, 0m1050 \le m \le 10^5, 0ci1090 \le c_i \le 10^9, 0vi1010×n0 \le v_i \le 10^{10} \times n. In each test point, the sum of all nn does not exceed 10002331\,000\,233, and the sum of all mm does not exceed 20002332\,000\,233.

Translated by ChatGPT 5