Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #define pb push_back
- using namespace std;
- const int inf = 1e9 + 7;
- const int maxn = 110;
- long long n, m, dp[maxn][maxn], a[maxn][maxn], mn = inf, x;
- vector <long long> v, cur;
- bool check(int i, int j) {
- return (i >= 1 && i <= n && j >= 1 && j <= m);
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- cin >> n >> m;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cin >> a[i][j];
- dp[i][j] = inf;
- }
- }
- for (int i = 1; i <= n; ++i) {
- dp[i][1] = a[i][1];
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j < m; ++j) {
- if (check(i + 1, j + 1)) {
- dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + a[i + 1][j + 1]);
- }
- if (check(i - 1, j + 1)) {
- dp[i - 1][j + 1] = min(dp[i - 1][j + 1], dp[i][j] + a[i - 1][j + 1]);
- }
- if (check(i, j + 1)) {
- dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[i][j + 1]);
- }
- }
- }
- /*for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cout << dp[i][j] << " ";
- }
- cout << endl;
- }*/
- for (int i = 1; i <= n; ++i) {
- if (dp[i][m] < mn) {
- mn = dp[i][m];
- x = i;
- }
- }
- //cout << mn << endl;
- for (int i = m; i >= 1; --i) {
- v.pb(x);
- if (dp[x - 1][i - 1] + a[x][i] == dp[x][i]) {
- --x;
- continue;
- }
- else if (dp[x][i - 1] + a[x][i] == dp[x][i]) {
- continue;
- }
- ++x;
- }
- reverse(v.begin(), v.end());
- for (int i = 0; i < v.size(); ++i) {
- cout << v[i] << " ";
- }
- cout << endl << mn;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement