fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. using namespace std;
  5.  
  6. const int R = 0;
  7. const int G = 1;
  8. const int B = 2;
  9. const int M_H = 1000;
  10.  
  11. //작은 수를 반환하는 함수
  12. inline int smaller(int a , int b)
  13. {
  14. return a<b ? a : b;
  15. }
  16. inline int smaller(int a , int b , int c)
  17. {//a가 가장 작? 참-> return a //거짓-> b가 가장 작냐? return b 아님 return c
  18. return (a<b&&a<c)? a : ((b<a&&b<c)? b : c);
  19. }
  20.  
  21. int main() {
  22. int h[M_H][3];
  23. int h_num; //집 수
  24. scanf(" %d", &h_num);
  25.  
  26. for(int i = 0 ; i < h_num ; i++)
  27. scanf("%d %d %d\n",&h[i][R],&h[i][G],&h[i][B]);
  28.  
  29. int memo[M_H][3]; //
  30. memo[0][R] = h[0][R]; memo[0][G] = h[0][G]; memo[0][B] = h[0][B];
  31.  
  32. for(int i = 1 ; i < h_num ; i++)
  33. {
  34. memo[i][R] = smaller(memo[i-1][G]+h[i][R] , memo[i-1][B]+h[i][R]);
  35. memo[i][G] = smaller(memo[i-1][R]+h[i][G] , memo[i-1][B]+h[i][G]);
  36. memo[i][B] = smaller(memo[i-1][R]+h[i][B] , memo[i-1][G]+h[i][B]);
  37. }
  38.  
  39. int answer = smaller(memo[h_num-1][R],memo[h_num-1][G],memo[h_num-1][B]);
  40.  
  41. printf("%d",answer);
  42. return 0;
  43. }
Success #stdin #stdout 0s 4372KB
stdin
2
1 1 1
1 1 2
stdout
3