#P16319. [ICPC 2023 Jinan R] 铁路环游
[ICPC 2023 Jinan R] 铁路环游
Problem Description
$\textbf{Please note that this problem has an unusual memory limit.}$
"Railway Tour" is a German-style board game themed around railways. In the game, players play train cards and build railways on the map. The score is determined by the total length of railways built and whether the player can connect faraway cities. The cities that need to be connected are determined by the drawn ticket cards.
:::align{center}

A photo taken by BoardGameGeek user @garyjames :::
Consider a one-dimensional version of the game. There are cities in a line, numbered from to from left to right. For each , you may place a railway between city and city to connect them.
There are ticket cards that reward the player for connecting cities. The -th card can be described by three integers , , and , meaning that if city and city can be connected by railways (that is, for all , there is a railway between city and city ), you will gain points.
For each , compute the maximum score when you place exactly railways. If you do not get any reward, your score is .
Input Format
There are multiple test cases. The first line contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and (), representing the maximum number of railways you may place and the number of ticket cards for rewards.
In the next lines, the -th line contains three integers , , and (, ), meaning that if city and city can be connected by railways, you will gain points.
It is guaranteed that the sum of all and the sum of all over all test cases are both at most .
Output Format
For each test case, output one line with integers separated by single spaces, where the -th integer represents the maximum score when you place exactly railways.
Please do not output extra spaces at the end of the line, otherwise your answer may be judged as wrong.
2
4 3
0 2 3
3 4 2
0 3 1
3 1
1 3 100
2 3 5 6
0 100 100
Hint
Let denote a railway between city and city . For the first sample test case:
- If you place railway, you can place and then get the second reward. The answer is .
- If you place railways, you can place and and then get the first reward. The answer is .
- If you place railways, you can place , , and and then get the first and second rewards. The answer is .
- If you place all railways, you can get all rewards. The answer is .
Translated by ChatGPT 5