Universal Cup Judging System

Universal Cup

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓
Estadísticas

耳廓狐 Sulfox 是一位程式設計師,喜歡在閒暇時間創作數位藝術。在經歷了一整天除錯的疲憊後,他像往常一樣打開繪圖軟體,卻發現軟體在最新更新後本身也充滿了 bug,其中一個主要問題是每當他編輯一個像素時就會明顯卡頓。

該繪圖軟體將畫布視為一個有序的圖層堆疊。每個圖層都是一個具有 $n$ 列 $m$ 行的像素網格,每個像素都擁有一個屬於 $\{0, 1, \dots, nm\}$ 的可見值,其中 $0$ 表示透明,而正數值則是 $nm$ 種不同顏色的識別碼。由於圖層可能會互相遮擋,在任何位置上,合成圖像的可見值等於所有圖層中最頂層非透明像素的可見值;如果所有圖層在該位置皆為透明,則可見值為 $0$。

Sulfox 已經非常清楚自己想畫什麼:一張由可見值矩陣 $(p_{i,j})_{n \times m}$ 表示的目標圖像。為了盡快完成繪製,他想要最小化由軟體 bug 造成的總「延遲成本」。從沒有任何圖層開始,Sulfox 可以執行以下操作任意多次:

  • 建立圖層(Create Layer):免費在最上方插入一個新圖層,並將其初始化為所有像素皆設為同一選定顏色 $c$($1 \le c \le nm$)的純色圖層,或是所有像素皆為透明的空白圖層;
  • 畫筆工具(Brush Tool):花費 $a$ 將任意圖層的任意單一像素設為任意選定顏色 $c$($1 \le c \le nm$);
  • 橡皮擦工具(Eraser Tool):花費 $b$ 將任意圖層的任意單一像素設為透明。

請幫助 Sulfox 求出執行操作所需的最小總成本,使得合成圖像與目標圖像相符(亦即兩張圖像在對應位置的可見值皆相等)。

輸入

輸入的第一行包含一個整數 $T$($1 \le T \le 10^5$),表示測試資料的組數。對於每組測試資料:

第一行包含四個整數 $n, m$($1 \le n, m \le 500$)、$a$ 與 $b$($1 \le a, b \le 10^6$),分別表示圖像尺寸、使用畫筆工具的成本,以及使用橡皮擦工具的成本。

接下來 $n$ 行中的第 $i$ 行包含 $m$ 個整數 $p_{i,1}, p_{i,2}, \dots, p_{i,m}$($0 \le p_{i,j} \le nm$),其中 $p_{i,j}$ 表示目標圖像在第 $i$ 列第 $j$ 行的可見值。

保證所有測試資料中 $nm$ 的總和不超過 $10^6$。

輸出

對於每組測試資料,輸出一行包含一個整數,代表最小總成本。

範例

範例輸入 1

3
1 2 3 2
0 1
2 2 1 1
1 0
2 3
3 3 5 3
2 4 4
4 1 4
4 4 2

範例輸出 1

2
3
11

提示

Sulfox

對於範例的第一組測試資料,Sulfox 可以先建立一個顏色為 $1$ 的圖層,然後花費 $b = 2$ 將第一列第一行的像素設為透明。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.