Advertisement
jeff69

Untitled

Apr 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <functional>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <vector>
  10. #include<set>
  11. #include<map>
  12. #include <time.h>
  13. #include <fstream>
  14. typedef long long ll;
  15. using namespace std;
  16. int a[3][200002];
  17. int n;
  18.  
  19.  
  20. ll dp[3][200002];
  21. ll solve(int x, int y){
  22. if (x == 1 && y == n - 1){
  23. return a[x][y];
  24. }
  25. else if (x<0 || x>2 || y >= n)return (1<<22);
  26. ll ret = (1 << 22);
  27. if (dp[x][y] != -1)return dp[x][y];
  28. ret = min(a[x][y] + solve(x + 1, y), ret);
  29. ret = min(a[x][y] + solve(x + 1, y + 1), ret);
  30. ret = min(a[x][y] + solve(x - 1, y + 1), ret);
  31. ret = min(a[x][y] + solve(x, y + 1),ret);
  32. dp[x][y] = ret;
  33. return ret;
  34. }
  35. int main(){
  36. while (1)
  37. {
  38.  
  39. cin >> n;
  40. if (n== 0)return 0;
  41.  
  42. for (int i = 0; i < n; i++)
  43. {
  44. for (int j = 0; j < 3; j++)
  45. {
  46. cin >> a[j][i];
  47. }
  48. }
  49. memset(dp, -1, sizeof (dp));
  50. cout << solve(1, 0);
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement