#P1653. [USACO04DEC] Cow Ski Area G

    ID: 2445 远端评测题 1000ms 256MiB 尝试: 1 已通过: 0 显示难度普及+/提高− 上传者: 标签>图论2004USACO强连通分量Tarjan

[USACO04DEC] Cow Ski Area G

Problem Description

John’s cousin Ron lives in Colorado. He plans to teach his cows to ski, but they are very shy and do not dare to ski at tourist resorts. So he has to build his own ski area. Ron’s ski area can be divided into WW columns and LL rows (1W500,1L500)(1 \le W \le 500,1 \le L \le 500). Each cell has a specific height H(0H9999)H(0 \le H \le 9999). Cows can ski between adjacent cells, and they are not allowed to move from a lower cell to a higher one.

To ensure that any two cells can reach each other, Ron plans to build some direct lifts. Lifts are powerful: they can connect any two cells and are bidirectional. Multiple lifts can be built at the same cell. However, lifts are very expensive, so he wants to build as few as possible. What is the minimum number of lifts needed?

Input Format

Line 1: Two space-separated integers: WW and LL.

Then input an L×WL×W matrix.

Output Format

Output the minimum number of lifts needed.

9 3
1 1 1 2 2 2 1 1 1
1 2 1 2 3 2 1 2 1
1 1 1 2 2 2 1 1 1
3

Hint

Constraints: 1W,L5001 \le W, L \le 500, 0H99990 \le H \le 9999.

Translated by ChatGPT 5.