Advertisement
LEGEND2004

DP matrix

Mar 14th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define fastio ios_base::sync_with_stdio(0); cin.tie(0)
  6.  
  7. const int N = 1e3 + 5;
  8.  
  9. int a[N][N] , dp[N][N];
  10.  
  11. signed main()
  12. {
  13.     fastio;
  14.  
  15.     int n , m;
  16.     cin >> n >> m;
  17.     for(int i = 1; i <= n; i++){
  18.         for(int j = 1; j <= m; j++){
  19.             cin >> a[i][j];
  20.         }
  21.     }
  22.     for(int i = 0; i <= n; i++){
  23.         for(int j = 0; j <= m; j++){
  24.             dp[i][j] = LLONG_MAX;
  25.         }
  26.     }
  27.     for(int i = 1; i <= n; i++){
  28.         for(int j = 1; j <= m; j++){
  29.             if(i == 1 && j == 1)
  30.                 dp[i][j] = a[i][j];
  31.             else
  32.                 dp[i][j] = min(dp[i - 1][j] , dp[i][j - 1]) + a[i][j];
  33.         }
  34.     }
  35.     cout << dp[n][m];
  36.  
  37.  
  38.     /*
  39.     1 2 1
  40.     1 9 9
  41.     9 9 1
  42.  
  43.     */
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement