Wesley's Anger Contest 5 Problem 7 - Acorn Delivery System

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 2.0s
Memory limit: 128M

Author:
Problem types

The squirrel nation is setting up an acorn delivery system. There are N squirrels numbered from 1 to N and each squirrel has a single acorn that they want to deliver to the head squirrel (who is squirrel number N). Specifically, they have designed a system where squirrel i will always pass their acorn, and any acorns they receive from other squirrels, to a single squirrel ji where i<jiN (with the exception of the head squirrel who will never pass an acorn to anyone).

The squirrels have scattered themselves randomly around their nation, independent of who they pass their acorns to, with the ith squirrel being located at the point (xi,yi). The additional cost of passing an acorn from squirrel i to squirrel j is equal to the Euclidean distance squared between their locations (xixj)2+(yiyj)2. In addition, if squirrel i receives an acorn (including the head squirrel), an additional cost of ci, chosen randomly and independent of who any squirrel passes their acorns to, is added.

The acorn originating from each squirrel will pass through a number of squirrels before reaching the head squirrel. In order to minimize the cost, for each acorn, you can choose for it to skip over a subset of the squirrels and move directly to the next unskipped squirrel, without modifying the original order. Of course, you cannot skip the head squirrel as they will be receiving the acorn, or the originating squirrel. For each squirrel, can you determine the minimum cost to deliver their acorn to the head squirrel?

For this problem, Python users are recommended to use PyPy over CPython.

Constraints

For this problem, NONE of the samples will appear in the actual test cases. In addition, all subtasks are disjoint, and you are NOT required to pass previous subtasks to earn points for a specific subtask.

For all subtasks:

1N200000
0xi,yi106 for all 1iN
0ci1012 for all 1iN
ci is selected uniformly at random from the range [0,1012] for all 1iN, independent of who any squirrel passes their acorns to.
i<jiN for all 1i<N
jN=0

Subtask 1 [8%]

1N16
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.

Subtask 2 [12%]

1N300
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.

Subtask 3 [6%]

1N5000
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
ji=i+1 for all 1i<N

Subtask 4 [4%]

1N5000
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.

Subtask 5 [16%]

1N200000
xi=0 for all 1iN
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
ji=i+1 for all 1i<N

Subtask 6 [15%]

1N200000
xi=0 for all 1iN
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.

Subtask 7 [17%]

1N200000
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
ji=i+1 for all 1i<N

Subtask 8 [22%]

1N200000
xi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.
yi is selected uniformly at random from the range [0,106] for all 1iN, independent of who any squirrel passes their acorns to.

Input Specification

The first line of input contains a single integer N, representing the number of squirrels in the squirrel nation.

The next N lines describe the squirrels. Each contains 4 integers, xi, yi, ci, ji, indicating that the ith squirrel is located at (xi,yi), incurs an additional cost of ci for every acorn that the ith squirrel receives, and passes their acorn to squirrel ji in the current system. Only input where jN=0 is allowed, which indicates that the head squirrel will not pass the acorn to any other squirrel.

Output Specification

This problem is graded with an identical checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n character and that there are no trailing spaces.

Output N lines. The ith line should contain a single integer equal to the minimum cost to deliver the ith squirrel's acorn to the head squirrel after skipping over a subset of the original squirrel.

Sample Input 1

Copy
5
0 1 3 2
0 3 1 3
0 7 2 4
0 5 10 5
0 4 3 0

Sample Output 1

Copy
9
4
12
4
0

Sample Explanation 1

Squirrel 1 can pass their acorn to squirrel 2, who can then pass the acorn to squirrel 5 for a cost of 22+1+12+3=9.
Squirrel 2 can pass their acorn to squirrel 5 (skipping squirrels 3 and 4) for a cost of 12+3=4.
Squirrel 3 can pass their acorn to squirrel 5 (skipping squirrel 4) for a cost of 32+3=12.
Squirrel 4 can pass their acorn to squirrel 5 for a cost of 12+3=4.
Squirrel 5 does not need to pass their acorn to anyone else, and thus incurs a cost of 0.

Sample Input 2

Copy
5
0 7 8 2
0 1 5 5
0 9 2 4
0 8 4 5
0 5 10 0

Sample Output 2

Copy
14
26
24
19
0

Sample Input 3

Copy
4
1 2 2 2
2 6 3 3
4 3 10 4
5 7 4 0

Sample Output 3

Copy
34
14
21
0

Sample Input 4

Copy
6
10 4 2 2
8 6 1 6
1 2 3 4
8 9 4 5
3 2 9 6
6 7 10 0

Sample Output 4

Copy
24
15
57
18
44
0

Comments

There are no comments at the moment.