ACwing 1018 最低通行费
该题思路与上题思路相似
**状态转移方程: **w[i] [j]为权值
f
[
i
]
[
j
]
=
M
a
t
h
.
m
i
n
(
f
[
i
−
1
]
[
j
]
,
f
[
i
]
[
j
−
1
]
)
+
w
[
i
]
[
j
]
;
f[i][j] = Math.min(f[i-1][j] , f[i][j-1])+w[i][j];
f[i][j]=Math.min(f[i−1][j],f[i][j−1])+w[i][j];
import java.util.*;
public class Main{
static int N = 105;
static int[][] g = new int[N][N];
static int[][] f = new int[N][N];
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 0 ; i <= n ; i++){
for(int j = 0 ; j<= n ;j++){
if(i>0 && j > 0 ) g[i][j] = in.nextInt();
if(i==0 || j == 0) f[i][j] = 0x3f3f3f3f;
}
}
f[1][0] = 0;
f[0][1] = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ;j++){
f[i][j] = Math.min(f[i-1][j] , f[i][j-1])+g[i][j];
}
}
System.out.println(f[n][n]);
}
}