#P13999. [eJOI 2025] Collecting Diamonds

    ID: 15787 远端评测题 3000ms 4MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP倍增2025交互题eJOI(欧洲)动态规划优化bitset

[eJOI 2025] Collecting Diamonds

题目描述

A diamond deposit has been discovered in the Rhodope Mountains. For simplicity, we will assume that the deposit has NN halls, labeled with integers from 00 to N1N-1. There are MM 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 (x,y)(x, y) such that yy cannot be reached from xx.

Petar will pass through KK corridors to mine diamonds. He will choose some hall ss to begin with, then move to a hall by passing through a corridor starting from ss, and so on until he has passed through exactly KK 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 KK corridors consecutively.

Petar will choose ss 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 KK 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)
  • NN: the number of halls in the diamond deposit;
  • MM: the number of corridors between the halls;
  • KK: the number of corridors Petar will pass;
  • uu, vv, dd: vectors of MM 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 NN, MM, and KK.
  • line 1+i1 + i: three integers u[i]u[i], v[i]v[i], d[i]d[i] – representing a corridor starting from hall u[i]u[i] and ending in hall v[i]v[i] with d[i]d[i] 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 N=5,M=6N=5,M=6 and K=4K=4:

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 N=5,M=5N=5,M=5 and K=4K=4:

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

  • 1N20001 \leq N \leq 2000
  • 1M40001 \leq M \leq 4000
  • 1K1091 \leq K \leq 10^9
  • 0u[i],v[i]<N0 \leq u[i], v[i] < N
  • 1d[i]1091 \leq d[i] \leq 10^9 for each 0i<M0 \leq i < M
  • 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 NN MM KK Additional constraints
0 00 - The examples.
1 1111 00 10\leq 10 20\leq 20 10\leq 10 -
2 1010 010-1 100\leq 100 1000\leq 1000 1000\leq 1000
3 2626 020-2 109\leq 10^9
4 1111 - 2000\leq 2000 =N=N Each hall has exactly one corridor starting from it and exactly one corridor ending in it.
5 1010 4000\leq 4000 All d[i]d[i] are distinct.
6 1111 There is exactly one d[i]=2d[i] = 2 (0i<M0 \leq i < M) and all other values in dd are equal to 1.
7 2121 060-6 -