/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
あなたは 2 次元平面上に存在する魔法戦士であり,最初は点 (0, 0) に位置します.
これから N 回のイベントが起こります.i 回目 (1 \leq i \leq N) のイベントでは点 (X_i, Y_i) にモンスターが 1 体現れ,あなたはこのモンスターを剣で倒すか魔法で倒すかを選択します.
- モンスターを剣で倒すことを選択した場合,あなたは点 (X_i, Y_i) に移動します.この際,あなたが移動する前に位置していた点と点 (X_i, Y_i) のユークリッド距離の 2 乗のコストがかかります.
- モンスターを魔法で倒すことを選択した場合,Z_i のコストがかかります.この際,移動は行いません.
最終的に必要なコストの総和の最小値を求めて下さい.
ユークリッド距離とは
点 (a, b) と点 (c, d) のユークリッド距離を \sqrt{(a - c)^2 + (b - d)^2} と定義します.1 つの入力ファイルにつき,T 個のテストケースを解いてください.
制約
- 1 \leq T \leq 10
- 1 \leq N \leq 250\,000
- 0 \leq X_i < 500 (1 \leq i \leq N)
- 0 \leq Y_i < 500 (1 \leq i \leq N)
- 1 \leq Z_i \leq 10^9 (1 \leq i \leq N)
- T 個のテストケースにおける N の総和は 250\,000 以下
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる.ここで,\mathrm{case}_i (1 \leq i \leq T) は i 番目のテストケースを意味する.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる.
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
出力
T 行出力せよ.i 行目 (1 \leq i \leq T) には \mathrm{case}_i に対する答えを出力せよ.
入力例 1
2 3 0 4 28 2 9 11 4 5 26 7 6 0 96 0 2 54 9 0 36 0 7 81 6 5 91 2 8 71 7 6 40
出力例 1
44 231
- 1 番目のテストケースについて,1 回目のイベントと 3 回目のイベントではモンスターを剣で倒すことを選択し,2 回目のイベントではモンスターを魔法で倒すことを選択するのが最適です.各イベントでは以下のように進行します.
- 1 回目のイベントでは,点 (0, 0) から点 (0, 4) に移動します.この際,コストが 16 かかります.
- 2 回目のイベントでは,コストが 11 かかります.
- 3 回目のイベントでは,点 (0, 4) から点 (4, 5) に移動します.この際,コストが 17 かかります.
- 最終的に必要なコストの総和は 16 + 11 + 17 = 44 です.
Score : 500 points
Problem Statement
You are a magic warrior on a two-dimensional plane, initially located at point (0, 0).
N events will now occur. In the i-th event (1 \leq i \leq N), one monster appears at point (X_i, Y_i), and you choose whether to defeat this monster with a sword or with magic.
- If you choose to defeat the monster with a sword, you move to point (X_i, Y_i). This incurs a cost equal to the square of the Euclidean distance between the point where you were located before moving and point (X_i, Y_i).
- If you choose to defeat the monster with magic, a cost of Z_i is incurred. In this case, you do not move.
Find the minimum total cost eventually required.
What is Euclidean distance?
The Euclidean distance between point (a, b) and point (c, d) is defined as \sqrt{(a - c)^2 + (b - d)^2}.Solve T test cases per input file.
Constraints
- 1 \leq T \leq 10
- 1 \leq N \leq 250\,000
- 0 \leq X_i < 500 (1 \leq i \leq N)
- 0 \leq Y_i < 500 (1 \leq i \leq N)
- 1 \leq Z_i \leq 10^9 (1 \leq i \leq N)
- The sum of N over the T test cases is at most 250\,000.
- All input values are integers.
Input
The input is given from Standard Input in the following format. Here, \mathrm{case}_i (1 \leq i \leq T) denotes the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
Output
Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for \mathrm{case}_i.
Sample Input 1
2 3 0 4 28 2 9 11 4 5 26 7 6 0 96 0 2 54 9 0 36 0 7 81 6 5 91 2 8 71 7 6 40
Sample Output 1
44 231
- For the first test case, it is optimal to choose to defeat the monster with a sword in the first and third events, and to choose to defeat the monster with magic in the second event. Each event proceeds as follows.
- In the first event, you move from point (0, 0) to point (0, 4). This incurs a cost of 16.
- In the second event, a cost of 11 is incurred.
- In the third event, you move from point (0, 4) to point (4, 5). This incurs a cost of 17.
- The total cost eventually required is 16 + 11 + 17 = 44.