Advertisement
pb_jiang

abc210d WA

Nov 16th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. // Problem: D - National Railway
  2. // Contest: AtCoder - AtCoder Beginner Contest 210
  3. // URL: https://atcoder.jp/contests/abc210/tasks/abc210_d
  4. // Memory Limit: 1024 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  13. template <typename... Args> void logger(string vars, Args &&... values)
  14. {
  15.     cerr << vars << " = ";
  16.     string delim = "";
  17.     (..., (cerr << delim << values, delim = ", "));
  18.     cerr << endl;
  19. }
  20.  
  21. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  22. using ll = long long;
  23. using pii = pair<int, int>;
  24.  
  25. ll h, w, c;
  26. vector<vector<int>> a;
  27. int main(int argc, char **argv)
  28. {
  29.     cin >> h >> w >> c;
  30.     a = vector<vector<int>>(h, vector<int>(w));
  31.     for (int i = 0; i < h; ++i)
  32.         for (int j = 0; j < w; ++j)
  33.             cin >> a[i][j];
  34.  
  35.     auto left_mat = vector<vector<ll>>(h, vector<ll>(w));
  36.     auto right_mat = vector<vector<ll>>(h, vector<ll>(w));
  37.  
  38.     for (int i = 0; i < h; ++i)
  39.         for (int j = 0; j < w; ++j)
  40.             left_mat[i][j] = min(j > 0 ? left_mat[i][j - 1] : LLONG_MAX, a[i][j] - c * (i + j)),
  41.             left_mat[i][j] = min(left_mat[i][j], i > 0 ? left_mat[i - 1][j] : LLONG_MAX);
  42.     for (int i = 0; i < h; ++i)
  43.         for (int j = w - 1; j >= 0; --j)
  44.             right_mat[i][j] = min(j < w - 1 ? right_mat[i][j + 1] : LLONG_MAX, a[i][j] - c * (i - j)),
  45.             right_mat[i][j] = min(right_mat[i][j], i > 0 ? right_mat[i - 1][j] : LLONG_MAX);
  46.     ll ans = LLONG_MAX;
  47.     for (int i = 0; i < h; ++i) {
  48.         for (int j = 0; j < w; ++j)
  49.             ans = min(ans, a[i][j] + c * (i + j) + (j > 0 ? left_mat[i][j - 1] : left_mat[i][j]));
  50.         for (int j = w - 1; j >= 0; --j)
  51.             ans = min(ans, a[i][j] + c * (i - j) + (j < w - 1 ? right_mat[i][j + 1] : right_mat[i][j]));
  52.     }
  53.     cout << ans << endl;
  54.     return 0;
  55. };
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement