Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. #define pb push_back
  7.  
  8. using namespace std;
  9.  
  10. const int inf = 1e9 + 7;
  11. const int maxn = 110;
  12.  
  13. long long n, m, dp[maxn][maxn], a[maxn][maxn], mn = inf, x;
  14. vector <long long> v, cur;
  15.  
  16. bool check(int i, int j) {
  17.     return (i >= 1 && i <= n && j >= 1 && j <= m);
  18. }
  19.  
  20. int main()
  21. {
  22.     //freopen("input.txt", "r", stdin);
  23.     //freopen("output.txt", "w", stdout);
  24.     cin >> n >> m;
  25.     for (int i = 1; i <= n; ++i) {
  26.         for (int j = 1; j <= m; ++j) {
  27.             cin >> a[i][j];
  28.             dp[i][j] = inf;
  29.         }
  30.     }
  31.     for (int i = 1; i <= n; ++i) {
  32.         dp[i][1] = a[i][1];
  33.     }
  34.     for (int i = 1; i <= n; ++i) {
  35.         for (int j = 1; j < m; ++j) {
  36.             if (check(i + 1, j + 1)) {
  37.                 dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + a[i + 1][j + 1]);
  38.             }
  39.             if (check(i - 1, j + 1)) {
  40.                 dp[i - 1][j + 1] = min(dp[i - 1][j + 1], dp[i][j] + a[i - 1][j + 1]);
  41.             }
  42.             if (check(i, j + 1)) {
  43.                 dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[i][j + 1]);
  44.             }
  45.         }
  46.     }
  47.     /*for (int i = 1; i <= n; ++i) {
  48.         for (int j = 1; j <= m; ++j) {
  49.             cout << dp[i][j] << " ";
  50.         }
  51.         cout << endl;
  52.     }*/
  53.     for (int i = 1; i <= n; ++i) {
  54.         if (dp[i][m] < mn) {
  55.             mn = dp[i][m];
  56.             x = i;
  57.         }
  58.     }
  59.     //cout << mn << endl;
  60.     for (int i = m; i >= 1; --i) {
  61.         v.pb(x);
  62.         if (dp[x - 1][i - 1] + a[x][i] == dp[x][i]) {
  63.             --x;
  64.             continue;
  65.         }
  66.         else if (dp[x][i - 1] + a[x][i] == dp[x][i]) {
  67.             continue;
  68.         }
  69.         ++x;
  70.     }
  71.     reverse(v.begin(), v.end());
  72.     for (int i = 0; i < v.size(); ++i) {
  73.         cout << v[i] << " ";
  74.     }
  75.     cout << endl << mn;
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement