Description
Rahul wanted to purchase vegetables mainly brinjal, carrot and tomato.
There are N different vegetable sellers in a line.
Each vegetable seller sells all three vegetable items, but at different prices. Rahul,
obsessed by his nature to spend optimally, decided not to purchase same vegetable from adjacent shops.
Also, Rahul will purchase exactly one type of vegetable item (only 1 kg) from one shop.
Rahul wishes to spend minimum money buying vegetables using this strategy.
Help Rahul determine the minimum money he will spend.
Input
First line indicates number of test cases T.
Each test case in its first line contains N denoting the number of vegetable sellers in Vegetable Market.
Then each of next N lines contains three space separated integers denoting cost of brinjal, carrot and tomato per kg with that particular vegetable seller.
Output
For each test case, output the minimum cost of shopping taking the mentioned conditions into account in a separate line.
Constraints:1 <= T <= 10,1 <= N <= 100000 Cost of each vegetable(brinjal/carrot/tomato) per kg does not exceed 10^4
Sample Input 1
1
3
1 50 50
50 50 50
1 50 50
Sample Output 1
52
# 不在相邻的超市买同样的菜
# 每个商店必须买一种菜
# 求最低价格
def solution(array):
# 数组每个值表示
init = [[0] * 3 for i in range(len(array))]
init[0][0] = array[0][0]
init[0][1] = array[0][1]
init[0][2] = array[0][2]
for i in range(1, len(array)):
for j in range(3):
init[i][j] = min(init[i - 1][(j + 1) % 3], init[i - 1][(j + 2) % 3]) + array[i][j]
res = min(init[-1][0], init[-1][1], init[-1][2])
return res
if __name__ == '__main__':
for _ in range(int(input())):
array = []
for _ in range(int(input())):
array.append(list(map(int, input().strip().split())))
result = solution(array)
print(result)