Universal Cup Judging System

Universal Cup

Süre Sınırı: 2.0 s Bellek Sınırı: 1024 MB Toplam puan: 100 Hack'lenebilir ✓
İstatistikler

耳廓狐 Sulfox 係一位程式員,平時鍾意喺空閒時間創作數碼藝術。經歷咗成日 debug 嘅疲勞之後,佢好似平時噉打開佢嘅繪圖軟件,點知發現個軟件喺最新一次更新之後都充滿咗 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.