如何花最少的钱购买蔬菜

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值