Skip to content

Instantly share code, notes, and snippets.

@ferbncode
Created March 29, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferbncode/4bc06ef5d2d2c4a8f33e8106c7aaa7ca to your computer and use it in GitHub Desktop.
Save ferbncode/4bc06ef5d2d2c4a8f33e8106c7aaa7ca to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
int costs[n][3];
for(int i=0;i<n;i++)
scanf("%d %d %d", &costs[i][0], &costs[i][1], &costs[i][2]);
int min_cost = 99999999;
int dp[n][3];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i=1;i<n;i++)
{
dp[i][0] = costs[i][0] + min(dp[i-1][1], dp[i-1][2]);
dp[i][1] = costs[i][1] + min(dp[i-1][0], dp[i-1][2]);
dp[i][2] = costs[i][2] + min(dp[i-1][1], dp[i-1][0]);
}
min_cost = min(dp[n-1][0], dp[n-1][1]);
min_cost = min(min_cost, dp[n-1][2]);
printf("%d\n", min_cost);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment