fork(3) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define LL long long
  5. LL MAX = (LL)1e18;
  6.  
  7. int main(){
  8. #ifndef ONLINE_JUDGE
  9. freopen("input.in","r",stdin);
  10. freopen("output.out","w",stdout);
  11. #endif
  12. int c=0;
  13. while(1){
  14. c++;
  15. int n;
  16. cin>>n;
  17. if(n==0){return 0;}
  18.  
  19. LL arr[n+1][4];
  20. for(int i=1;i<=n;i++){
  21. for(int j=1;j<=3;j++){
  22. cin>>arr[i][j];
  23. }
  24. }
  25. LL dp[n+1][4];
  26. for(int i=0;i<=n;i++){for(int j=0;j<4;j++){dp[i][j] = MAX;}}
  27.  
  28. dp[1][2] = arr[1][2];
  29. for(int i=2;i<=n;i++){
  30. for(int j=1;j<4;j++){
  31. dp[i][j] = min(dp[i][j-1],min(dp[i-1][j-1],min(dp[i-1][j],dp[i-1][j+1])));
  32. dp[i][j]+=arr[i][j];
  33. }
  34. }
  35. cout<<c<<". "<<dp[n][2]<<endl;
  36. }
  37. }
Success #stdin #stdout 0s 15240KB
stdin
4
13 7 5
7 13 6
14 3 12
15 6 16
0
stdout
1. 22