CF1520G.To Go Or Not To Go?

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Dima overslept the alarm clock, which was supposed to raise him to school.

Dima wonders if he will have time to come to the first lesson. To do this, he needs to know the minimum time it will take him to get from home to school.

The city where Dima lives is a rectangular field of n×mn \times m size. Each cell (i,j)(i, j) on this field is denoted by one number aija_{ij}:

  • The number −1-1 means that the passage through the cell is prohibited;
  • The number 00 means that the cell is free and Dima can walk though it.
  • The number xx (1≤x≤1091 \le x \le 10^9) means that the cell contains a portal with a cost of xx. A cell with a portal is also considered free.

From any portal, Dima can go to any other portal, while the time of moving from the portal (i,j)(i, j) to the portal (x,y)(x, y) corresponds to the sum of their costs aij+axya_{ij} + a_{xy}.

In addition to moving between portals, Dima can also move between unoccupied cells adjacent to one side in time ww. In particular, he can enter a cell with a portal and not use it.

Initially, Dima is in the upper-left cell (1,1)(1, 1), and the school is in the lower right cell (n,m)(n, m).

季马睡过头了,没能听到本该叫他去上学的闹钟。

季马想知道他能否及时赶到第一节课堂。为此,他需要知道从家到学校所需的最短时间。

季马所居住的城市是一个大小为 n×mn \times m 的矩形网格。该网格中每个单元格 (i,j)(i, j) 用一个整数 aija_{ij} 表示:

  • 数字 −1-1 表示该单元格禁止通行;
  • 数字 00 表示该单元格为空闲状态,季马可以自由通过;
  • 数字 xx(其中 1≤x≤1091 \le x \le 10^9)表示该单元格中存在一个传送门,其使用代价为 xx;含有传送门的单元格同样被视为可通行。

从任意一个传送门出发,季马均可直接传送到任意其他传送门,且从传送门 (i,j)(i, j) 传送到传送门 (x,y)(x, y) 所需时间为二者代价之和:aij+axya_{ij} + a_{xy}。

除传送门外,季马还可花费时间 ww 在相邻(上下左右四邻)的空闲单元格之间移动。特别地,他可以进入一个含传送门的单元格但选择不使用该传送门。

初始时,季马位于左上角单元格 (1,1)(1, 1),而学校位于右下角单元格 (n,m)(n, m)。

输入格式

The first line contains three integers nn, mm and ww (2≤n,m≤2⋅1032 \le n, m \le 2 \cdot 10^3, 1≤w≤1091 \le w \le 10^9), where nn and mm are city size, ww is time during which Dima moves between unoccupied cells.

The next nn lines each contain mm numbers (−1≤aij≤109-1 \le a_{ij} \le 10^9) — descriptions of cells.

It is guaranteed that the cells (1,1)(1, 1) and (n,m)(n, m) are free.

第一行包含三个整数 nn、mm 和 ww(2≤n,m≤2⋅1032 \le n, m \le 2 \cdot 10^3,1≤w≤1091 \le w \le 10^9),其中 nn 和 mm 表示城市的尺寸,ww 表示迪马在未被占据的格子之间移动所需的时间。

接下来的 nn 行,每行包含 mm 个数字(−1≤aij≤109-1 \le a_{ij} \le 10^9),描述各个格子。

保证格子 (1,1)(1, 1) 和 (n,m)(n, m) 是空闲的。

输出格式

Output the minimum time it will take for Dima to get to school. If he cannot get to school at all, then output "-1".

输出迪马到达学校所需的最短时间。如果他根本无法到达学校,则输出“-1”。

输入输出样例

  • 输入#1

    5 5 1
    0 -1 0 1 -1
    0 20 0 0 -1
    -1 -1 -1 -1 -1
    3 0 0 0 0
    -1 0 0 0 0

    输出#1

    14

说明/提示

Explanation for the first sample:

第一个样例的解释:

输入解题思路,AI测评打分。不知道怎么写?

首页