Advertisement
jeff69

Untitled

Apr 3rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 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. 4
  20. 13 7 5
  21. 7 13 6
  22. 14 3 12
  23. 15 6 16
  24. 0
  25. */
  26.  
  27. ll dp[3][200002];
  28. ll solve(int x, int y){
  29. if (x == 1 && y == n - 1){
  30. return a[x][y];
  31. }
  32. else if (x<0 || x>2 || y >= n)return (1 << 22);
  33. ll ret = (1 << 22);
  34. if (dp[x][y] != -1)return dp[x][y];
  35. ret = min(a[x][y] + solve(x + 1, y + 1), ret);
  36.  
  37. ret = min(a[x][y] + solve(x - 1, y + 1), ret);
  38. ret = min(a[x][y] + solve(x, y + 1), ret);
  39. ret = min(a[x][y] + solve(x + 1, y), ret);
  40. dp[x][y] = ret;
  41. return ret;
  42. }
  43. int main(){
  44. int t = 0;
  45. while (1)
  46. {
  47. t++;
  48.  
  49. cin >> n;
  50. if (n == 0)return 0;
  51.  
  52. for (int i = 0; i < n; i++)
  53. {
  54. for (int j = 0; j < 3; j++)
  55. {
  56. cin >> a[j][i];
  57. }
  58. }
  59. memset(dp, -1, sizeof (dp));
  60. for (int i = n - 1; i>0; --i)solve(1, i);
  61. cout << t<<". "<<solve(1, 0);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement