#P13999. [eJOI 2025] Collecting Diamonds
[eJOI 2025] Collecting Diamonds
题目描述
A diamond deposit has been discovered in the Rhodope Mountains. For simplicity, we will assume that the deposit has halls, labeled with integers from to . There are one-way corridors connecting some of the halls such that there is at least one corridor going out of each hall. Each corridor has a certain number of diamonds that can be mined when passing through it. This number does not change when passing through the corridor – it remains the same for subsequent passes.
It is possible that a corridor connects a hall to itself, and there may be multiple corridors between the same pair of halls (possibly in the same direction). It is also not guaranteed that the halls are connected; i.e., there may be a pair of halls such that cannot be reached from .
Petar will pass through corridors to mine diamonds. He will choose some hall to begin with, then move to a hall by passing through a corridor starting from , and so on until he has passed through exactly corridors. Note that he can repeat halls and corridors, and that the number of diamonds he collects from a corridor does not change upon repetition. Notice that there is always going to be a way for him to pass through corridors consecutively.
Petar will choose and the path he will follow in the following way: First, he wants to maximize the number of diamonds he will collect from the first corridor he passes through. Among all such options, he will choose the one that maximizes the number of diamonds he will collect from the second corridor. This repeats times. In other words, Petar wants to choose a lexicographically greatest path. He wonders what the total number of diamonds he will collect is if he chooses such a path. Help him calculate this.
Implementation details
You should implement the function calculate_diamonds
:
long long int calculate_diamonds(int N, int M, int K, std::vector<int> u, std::vector<int> v, std::vector<int> d)
- : the number of halls in the diamond deposit;
- : the number of corridors between the halls;
- : the number of corridors Petar will pass;
- , , : vectors of integers, representing the starting halls, ending halls, and diamonds for the corridors.
This function will be called once for each test and has to return one number – the total number of diamonds Petar will collect using his strategy.
输入格式
The input format is the following:
- line 1: three integers – the values of , , and .
- line : three integers , , – representing a corridor starting from hall and ending in hall with diamonds for mining.
输出格式
The output format is the following:
- line 1: one integer – the return value of the call.
5 6 4
2 0 12
0 4 8
4 1 9
2 3 12
3 1 8
1 4 10
39
5 5 4
0 1 7
1 0 6
2 3 7
3 4 7
4 2 1
22
提示
Example 1
Consider the following call and illustration, for and :
calculate_diamonds(5, 6, 4, {2, 0, 4, 2, 3, 1}, {0, 4, 1, 3, 1, 4}, {12, 8, 9, 12, 8, 10})
:::align{center}
:::
Petar will choose to pass through the following corridors: $2 \overset{12}{\rightarrow} 3 \overset{8}{\rightarrow} 1 \overset{10}{\rightarrow} 4 \overset{9}{\rightarrow} 1$. The total number of diamonds he will collect is 39, which should be the value returned by the call.
Example 2
Consider the following call and illustration, for and :
calculate_diamonds(5, 5, 4, {0, 1, 2, 3, 4}, {1, 0, 3, 4, 2}, {7, 6, 7, 7, 1})
:::align{center}
:::
There are 5 options for passing through 4 corridors:
(1) $0 \overset{7}{\rightarrow} 1 \overset{6}{\rightarrow} 0 \overset{7}{\rightarrow} 1 \overset{6}{\rightarrow} 0$;
(2) $1 \overset{6}{\rightarrow} 0 \overset{7}{\rightarrow} 1 \overset{6}{\rightarrow} 0 \overset{7}{\rightarrow} 1$;
(3) $2 \overset{7}{\rightarrow} 3 \overset{7}{\rightarrow} 4 \overset{1}{\rightarrow} 2 \overset{7}{\rightarrow} 3$;
(4) $3 \overset{7}{\rightarrow} 4 \overset{1}{\rightarrow} 2 \overset{7}{\rightarrow} 3 \overset{7}{\rightarrow} 4$;
(5) $4 \overset{1}{\rightarrow} 2 \overset{7}{\rightarrow} 3 \overset{7}{\rightarrow} 4 \overset{1}{\rightarrow} 2$.
Options (2) and (5) do not maximize the number of diamonds from the first corridor. From options (1), (3), and (4) only option (3) maximizes the number of diamonds from the second corridor so this is the best option for Petar. Note that option (3) does not maximize the number of diamonds from the third corridor, nor does it maximize the total number of diamonds, but it is the only lexicographically greatest sequence. The total number of diamonds Petar will collect is 22, which should be the value returned by the call.
Constraints
- for each
- It is guaranteed that there is at least one corridor starting from each hall.
- Notice the unusually small memory limit of 4 MB.
Subtasks
Subtask | Points | Required subtasks | Additional constraints | |||
---|---|---|---|---|---|---|
0 | - | The examples. | ||||
1 | - | |||||
2 | ||||||
3 | ||||||
4 | - | Each hall has exactly one corridor starting from it and exactly one corridor ending in it. | ||||
5 | All are distinct. | |||||
6 | There is exactly one () and all other values in are equal to 1. | |||||
7 | - |