Advertisement
jeff69

Untitled

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