QOJ.ac

QOJ

時間限制: 2.0 s 記憶體限制: 512 MB 總分: 100

#18021. Hristenko Oleg

统计

给你一个 $n \times m$ 的整数网格。

考虑以下图:网格中的每个单元格都视为一个顶点。如果两个单元格位于同一行或同一列,则它们之间连有一条边,边的权值等于这两个单元格中数值的绝对差。

考虑该图的最小生成树(生成树的权值为其中所有边权之和)。求该最小生成树的权值。

输入格式

第一行包含两个整数 $n$ 和 $m$($1 \le n \cdot m \le 10^5$),分别表示网格的行数和列数。

接下来 $n$ 行,其中第 $i$ 行包含 $m$ 个整数 $a_{i,j}$($0 \le a_{i,j} < 10^9$),表示网格中的元素。

输出格式

输出一个整数,表示最小生成树的权值。

样例

输入样例 1

2 3
0 2 4
3 4 5

输出样例 1

7

输入样例 2

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

输出样例 2

16

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.