-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path58tc3.cpp
77 lines (72 loc) · 1.41 KB
/
58tc3.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
using namespace std;
// greedy
int MinCost(const vector<vector<int>>& mat)
{
int row = static_cast<int>(mat.size());
int col = static_cast<int>(mat[0].size());
int i = 0;
int j = 0;
int ret = 0;
while (i < row-1 && j < col-1)
{
ret += mat[i][j];
if (mat[i+1][j] < mat[i][j+1])
{
++i;
}
else
{
++j;
}
}
if (i == row-1)
{
for (int x = j; x <= col-1; ++x)
ret += mat[i][x];
}
else
{
for (int x = i; x <= row-1; ++x)
ret += mat[x][j];
}
return ret;
}
int mincost(const vector<vector<int>>& mat)
{
int row = static_cast<int>(mat.size());
int col = static_cast<int>(mat[0].size());
vector<vector<int>> dp(row, vector<int>(col, 0));
// initialize
dp[0][0] = mat[0][0];
for (int i = 1; i < row; ++i)
dp[i][0] = dp[i-1][0] + mat[i][0];
for (int j = 1; j < col; ++j)
dp[0][j] = dp[0][j-1] + mat[0][j];
for (int i = 1; i < row; ++i)
{
for (int j = 1; j < col; ++j)
{
dp[i][j] = mat[i][j] + min(dp[i-1][j], dp[i][j-1]);
}
}
return dp[row-1][col-1];
}
int main()
{
int num_rows, num_cols;
cin >> num_rows >> num_cols;
vector<vector<int>> mat(num_rows, vector<int>(num_cols, 0));
for (int i = 0; i != num_rows; ++i)
{
for (int j = 0, x; j != num_cols; ++j)
{
cin >> x;
mat[i][j] = x;
}
}
// io done
cout << mincost(mat);
return 0;
}