#P13070. [GCJ 2020 Finals] Pack the Slopes

    ID: 12880 Type: RemoteJudge 30000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>2020线段树树链剖分Google Code Jam

[GCJ 2020 Finals] Pack the Slopes

题目描述

You are trying to organize a group of skiers. The skiers are taking a trip to a large mountain, which has been rented for the day.

There are N\mathbf{N} rest points numbered from 1 to N\mathbf{N} on the mountain, and they are connected by N1\mathbf{N}-1 slopes. Each slope starts at some rest point and goes directly to another rest point, with no intermediate slopes or rest points. A slope can be traversed in only one direction.

Each skier starts at the summit rest point and traverses a slope to reach another rest point. From there, the skier can traverse another slope to reach another rest point, and so on. Once a skier reaches their destination rest point, they stop skiing for the day and head to the ski lodge for hot cocoa. The destination rest point cannot be the summit rest point. However, notice that a skier's destination rest point can be the start of zero or more slopes; that is, a skier does not necessarily have to keep using available slopes until there are none available: they can always walk carefully down the rest of the mountain! For all rest points, there is exactly one sequence of slopes that a skier can use to reach it from the summit rest point.

Each slope can accommodate only a certain total number of skiers in a day, after which the snow gets too choppy to ski. In addition, the ski resort can charge or reward each skier for each slope that they ski on. Each slope may have a different price, and each skier must pay the price for each slope they ski on. A slope's price can be positive, zero, or even negative; a negative price represents a bounty awarded for testing that slope. As the organizer, you pay all the slope prices and collect all the bounties on behalf of your group of skiers. Notice that if multiple skiers use the same slope, you pay that slope's price or collect the slope's bounty multiple times. The sum of all costs you pay minus the sum of all bounties you collect is the total expense for the trip. The expense can be positive, zero, or negative. A negative expense means that you actually made money on the trip!

As the organizer, you want to figure out the maximum number of skiers that you can put on the mountain. Also, you would like to figure out the minimum possible expense for a trip with that maximum number of skiers.

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. The first line of a test case contains a single integer N\mathbf{N}: the number of rest points on the mountain.

Each of the final N1\mathbf{N}-1 lines of a test case describes a slope via four integers Ui\mathbf{U_i}, Vi\mathbf{V_i}, Si\mathbf{S_i}, and Ci\mathbf{C_i}. These are the slope's starting rest point, the slope's ending rest point, the maximum number of skiers the slope can accommodate, and the slope's price per skier, respectively.

The summit rest point where the skiers start from is always numbered 1.

输出格式

For each test case, output one line containing Case #x: y z, where xx is the test case number (starting from 1), yy is the maximum number of skiers, and zz is the minimum expense for having yy skiers ski at least one slope each.

2
4
1 2 2 5
1 3 2 5
3 4 1 -2
7
4 7 2 2
1 3 5 5
1 4 2 -1
3 2 3 -2
3 5 2 -1
3 6 2 2
Case #1: 4 18
Case #2: 7 15

提示

Sample Explanation

In Sample Case #1, we can send one skier to rest point 4, one skier to rest point 3, and two skiers to rest point 2.

In Sample Case #2, we can send three skiers to rest point 2, two skiers to rest point 5, and two skiers to rest point 4.

Notice that the first slope listed in a test case does not need to start at the summit rest point, and that slopes can have Ui>Vi\mathbf{U_i} > \mathbf{V_i}.

Limits

  • 1UiN1 \leqslant \mathbf{U_i} \leqslant \mathbf{N}, for all ii.
  • 2ViN2 \leqslant \mathbf{V_i} \leqslant \mathbf{N}, for all ii. (No slope can end at the summit rest point.)
  • UiVi\mathbf{U_i} \neq \mathbf{V_i}, for all ii.
  • 1Si1051 \leqslant \mathbf{S_i} \leqslant 10^5, for all ii.
  • 105Ci105-10^5 \leqslant \mathbf{C_i} \leqslant 10^5, for all ii.
  • There is exactly one sequence of slopes that a skier can use to reach rest point rr from the summit rest point, for all rr.

Test Set 1 (10 Pts, Visible Verdict)

  • 1T1001 \leqslant \mathbf{T} \leqslant 100.
  • 2N10002 \leqslant \mathbf{N} \leqslant 1000.

Test Set 2 (22 Pts, Hidden Verdict)

  • T=17\mathbf{T} = 17.
  • 2N1052 \leqslant \mathbf{N} \leqslant 10^5.