#P15454. [JOI 2026 SemiFinal] 川下り / River Rafting
[JOI 2026 SemiFinal] 川下り / River Rafting
Problem Description
In the country of JOI, there are towns, numbered from to . Between these towns there are roads, numbered from to . Road () bidirectionally connects town () and town . It is guaranteed that starting from town , you can reach any town by traveling along some roads.
In addition, JOI has rivers parallel to the roads, numbered from to . River () is parallel to road , and flows from town to town .
Each of the towns has a lamp. Each lamp has an intensity. If the lamp at town () has intensity , then all towns that can be reached from town by traveling along fewer than roads are illuminated by this lamp. Initially, all lamps have intensity , and no town is illuminated.
You may perform the operation “going downstream” any number of times (including times). Each time you go downstream, you start from being at town , and first increase the lamp intensity at town by . Then, you repeatedly perform the following steps in order:
- Decide whether to end this downstream trip. However, if there is no outgoing river from the current town, you must end.
- If you continue going downstream, choose one river among the rivers flowing out of the current town, and move along that river. After moving, increase the lamp intensity at the town you arrive at by .
If you end the downstream trip at town , then the cost of this downstream trip is . You want to perform some number of downstream trips so that every town is illuminated by at least one lamp. Under this condition, minimize the total cost of all downstream trips.
Input Format
Input is given from standard input in the following format:
Output Format
Print, in one line, the minimum possible total cost of downstream trips required to make every town illuminated by at least one lamp.
5
1 2 2 4
10 4 8 9 5
9
9
1 1 1 2 5 5 5 3
100 70 80 90 60 30 40 50 30
90
Hint
Sample Explanation 1
In the first downstream trip, choose river and end at town . Then, the lamp intensities of towns each increase by , and the cost is . In the second downstream trip, choose rivers and end at town . Then, the lamp intensities of towns each increase by , and the cost is .
After these operations, the lamp intensity of towns is , the lamp intensity of town is , and the lamp intensity of towns is . Towns are illuminated by the lamp at town with intensity , and town is illuminated by the lamp at town with intensity . Therefore, after these operations, all towns are illuminated by some lamp. The total cost is . It is impossible to satisfy the condition with a cost smaller than , so output .
This input sample satisfies the Constraints of subtasks .
Sample Explanation 2
Perform two downstream trips that go along rivers and end at town , and one downstream trip that goes along rivers and ends at town . After these operations, the lamp intensity of town is ; the lamp intensities of towns are ; the lamp intensities of towns are ; and the lamp intensities of towns are . Through these operations, all towns are illuminated by some lamp. The total cost is . It is impossible to satisfy the condition with a cost smaller than , so output .
This input sample satisfies the Constraints of subtasks .
Constraints
- ()
- ()
- All input values are integers.
Subtasks
- (13 points)
- (25 points)
- (7 points) ()
- (11 points) ()
- (16 points) For each (), the number of () such that is at most
- (28 points) No additional constraints
Translated by DeepSeek.
Translated by ChatGPT 5