1517D - Explorer Space - CodeForces Solution


dp graphs shortest paths *1800

Please click on ads to support us..

Python Code:

n, m, k = map(int, input().split())
 
h = []
for i in range(n):
  h.append(list(map(int, input().split())))
 
v = []
for i in range(n - 1):
  v.append(list(map(int, input().split())))
 
if k % 2 == 0:
  d = [[0] * m for i in range(n)]
  for t in range(k // 2):
    dt = [[0] * m for i in range(n)]
    for i in range(n):
      for j in range(m):
        x = float('inf')
        if i - 1 >= 0:
          x = min(x, d[i - 1][j] + v[i - 1][j] * 2)
        if i + 1 < n:
          x = min(x, d[i + 1][j] + v[i][j] * 2)
        if j - 1 >= 0:
          x = min(x, d[i][j - 1] + h[i][j - 1] * 2)
        if j + 1 < m:
          x = min(x, d[i][j + 1] + h[i][j] * 2)
        dt[i][j] = x
    d = dt.copy()
else:
  d = [[-1] * m for i in range(n)]
for i in d:
    print(*i)

C++ Code:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr);

    int n, m, k;
    cin >> n >> m >> k;

    vector <vector <int>> h(n, vector <int> (m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m - 1; ++j) {
            cin >> h[i][j];
        }
    }
    vector <vector <int>> v(n, vector <int> (m));
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> v[i][j];
        }
    }

    if (k & 1) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cout << -1 << " \n"[j + 1 == m];
            }
        }
    }

    else {
        vector <vector <vector<ll>>> dist(n, 
                vector <vector <ll>>     (m, 
                        vector <ll> ((k >> 1) + 1, INT_MAX)));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                dist[i][j][0] = 0;
            }
        }
        for (int kk = 1; kk <= k / 2; ++kk) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    if (i > 0) {
                        dist[i][j][kk] = min(dist[i][j][kk], dist[i - 1][j][kk - 1] + v[i - 1][j]);
                    } 
                    if (j > 0) {
                        dist[i][j][kk] = min(dist[i][j][kk], dist[i][j - 1][kk - 1] + h[i][j - 1]);
                    }
                    if (i < n - 1) {
                        dist[i][j][kk] = min(dist[i][j][kk], dist[i + 1][j][kk - 1] + v[i + 0][j]);
                    }
                    if (j < m - 1) {
                        dist[i][j][kk] = min(dist[i][j][kk], dist[i][j + 1][kk - 1] + h[i][j + 0]);
                    }
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cout << 2 * dist[i][j][k / 2] << " \n"[j + 1 == m];
            }
        }
    }

}


Comments

Submit
0 Comments
More Questions

1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks