luogu#P4775. [NOI2018] 情报中心
[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 nodes, connected by 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 possible plans to set up intelligence agencies. In the -th plan, intelligence personnel are deployed on all edges along the shortest path from node to node to collect intelligence, and this plan costs yuan.
However, due to lack of manpower, GGB can only implement two of the above 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 , meaning that collecting intelligence on this edge brings a profit of yuan. Note that intelligence is unique, so even if an edge’s intelligence is collected by both agencies, the profit is still only .
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 , indicating the number of test cases.
Each test case contains lines:
Line contains an integer , the number of nodes in the city.
Lines to (i.e., line ) each contain three integers , , , describing a bidirectional edge connecting nodes and with intelligence value . It is guaranteed that and all are distinct.
Line contains an integer , the number of plans proposed by TAC.
Lines to (i.e., line ) each contain three integers , , , meaning that the -th plan deploys intelligence personnel on all edges along the shortest path from node to node , and costs yuan.
Output Format
Write output to file center.out.
The output file contains 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 to node in plan 1 is , and the shortest path from node to node in plan 2 is . Choosing these two plans costs , and intelligence on every edge is collected, yielding a profit of , so (total profit minus total cost) is .
- For the second test case, the shortest path from node to node in plan 1 is , and the shortest path from node to node in plan 2 is . 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 and plan .
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 ; blue edges are collected only by the intelligence center in plan ; 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 . 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 , 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 | Special Property | |||
|---|---|---|---|---|
| 1 | Guaranteed | None | ||
| 2 | ^ | ^ | ||
| 3 | ||||
| 4 | ||||
| 5 | ^ | |||
| 6 | ||||
| 7 | ||||
| 8 | ^ | |||
| 9 | ^ | |||
| 10 | ||||
| 11 | ^ | Not guaranteed | ^ | |
| 12 | ^ | ^ | ||
| 13 | Guaranteed | |||
| 14 | ^ | ^ | ||
| 15 | Not guaranteed | |||
| 16 | ^ | |||
| 17 | Guaranteed | None | ||
| 18 | ^ | ^ | ||
| 19 | ^ | Not guaranteed | ||
| 20 | ^ | |||
The special properties in the table are:
-
Special property : For any , it is guaranteed that the smallest-indexed node on the shortest path from to is different from the smallest-indexed node on the shortest path from to .
-
Special property : For any , it is guaranteed that the smallest-indexed node on the shortest path from to is node .
For all testdata, , , , . In each test point, the sum of all does not exceed , and the sum of all does not exceed .
Translated by ChatGPT 5