forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumPathSum.cpp
45 lines (42 loc) · 924 Bytes
/
MinimumPathSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
typedef long long int lli;
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(50);
lli n,m;
cin >> n >> m;
lli a[n][m];
lli dp[n][m];
for(int i = 0; i<n; i++)
{
for(int j = 0; j<m; j++)
{
cin >> a[i][j];
dp[i][j] = 1e18;
}
}
dp[0][0] = a[0][0];
for(int i = 0; i<n; i++)
{
for(int j = 0; j<m; j++)
{
if(j-1 >= 0)
{
dp[i][j] = min(dp[i][j],dp[i][j-1] + a[i][j]);
}
if(i-1 >= 0)
{
dp[i][j] = min(dp[i][j],dp[i-1][j] + a[i][j]);
}
if(i-1 >= 0 && j-1 >= 0)
{
dp[i][j] = min(dp[i][j],dp[i-1][j-1] + a[i][j]);
}
}
}
cout << dp[n-1][m-1] << endl;
}