fork download
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. int main(){
  7. int t;
  8. cin >> t;
  9. while(t--){
  10. int n, m, k;
  11. cin >> n >> m >> k;
  12.  
  13. vector<vector<ll> > v(n, vector<ll>(m));
  14. vector<vector<ll> > dp(n, vector<ll>(m));
  15.  
  16. for(int i = 0; i < n; i++){
  17. for(int j = 0; j < m; j++){
  18. cin >> v[i][j];
  19.  
  20. dp[i][j] = v[i][j];
  21. if(i > 0 && j > 0) dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
  22. else if(i > 0) dp[i][j] += dp[i - 1][j];
  23. else if(j > 0) dp[i][j] += dp[i][j - 1];
  24. }
  25.  
  26. for(int l = 0; l < m; l++){
  27. vector<ll> temp(m, 1e17);
  28. for(int j = 0; j < m; j++){
  29. if(j > 0) temp[j] = min(v[i][(j + l)%m] + temp[j - 1], temp[j]);
  30. if(i > 0) temp[j] = min(v[i][(j + l)%m] + dp[i - 1][j] + k*l, temp[j]);
  31. else temp[j] = v[i][l] + k*l;
  32. dp[i][j] = min(temp[j], dp[i][j]);
  33. }
  34. }
  35. }
  36. cout<<dp[n-1][m-1]<<endl;
  37. }
  38. }
Success #stdin #stdout 0s 5276KB
stdin
5
3 3 100
3 4 9
5 2 4
0 101 101
3 4 1
10 0 0 10
0 0 10 0
10 10 0 10
1 1 3
4
3 2 3
1 2
3 6
5 4
10 10 14
58 49 25 12 89 69 8 49 71 23
45 27 65 59 36 100 73 23 5 84
82 91 54 92 53 15 43 46 11 65
61 69 71 87 67 72 51 42 55 80
1 64 8 54 61 70 47 100 84 50
86 93 43 51 47 35 56 20 33 61
100 59 5 68 15 55 69 8 8 60
33 61 20 79 69 51 23 24 56 28
67 76 3 69 58 79 75 10 65 63
6 64 73 79 17 62 55 53 61 58

stdout
107
4
4
11
334