NJU-高级算法-如何花最少的钱购买蔬菜

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
3
1 50 50
50 50 50
1 50 50

Sample Output:

52

 

思路

原题来自GeeksForGeeks: Buying Vegetables

动态规划

1.确定状态

题中要求不能在相邻的两家店买同样的商品,如在第一家商店买了胡萝卜,那么就不能在第二家买胡萝卜,但可以在第三家买胡萝卜.设有n家商店,状态矩阵为dp[n][3],3表示这里题中规定好范围的3种商品. dp[i][j]表示在第i+1家买第j+1种商品时的累计最小花费(如dp[2][1]表示在第3家商店买第2种商品时的累计最小花费).

dp[i][j] = getMinCostOfLastRow(dp[i - 1], j) + cost[i][j]

其中getMinCostOfLastRow(dp[i - 1], j)表示在当前商店的前一个商店买除了第j+1个商品的最小累计化费.

dp[i][j]即在当前商店(第i+1个商店)买第j+1个商品时的最小累计花费,等于在当前商店的前一个商店买除了第j+1个商品的最小化费加上买当前商品的单价.

如dp[2][1] = getMinCostOfLastRow(dp[1], 1) + cost[2][1],表示在第3家商店买第2个商品的最小累计花费为在第2家商店买除了第2个商品外其他商品的最小累计花费加上在第3家商店买第2个商品的单价.

2.初始化

对dp矩阵第一行进行初始化,dp[0][j]即在第1家商店购买第j+1种商品的花费.

3.边界情况

dp矩阵最后一行中的最小值即为该问题的解

4.计算顺序

自上而下,从左到右(重点是自上而下,每一行代表一家商店,每次计算需要在前一行即前一家商店的情况的基础上进行).

 

代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    // 找到last数组中除cur位置元素外的最大值
    private static int getMinCostOfLastRow(int[] last, int cur) {
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < 3; i++) {
            if (i == cur) {
                continue;
            }
            res = last[i] < res ? last[i] : res;
        }
        return res;
    }

    private static int getMinCost(int[][] cost, int n) {
        int dp[][] = new int[n][3];

        // 初始化,dp矩阵第一行就是在第一家买各个商品的花费
        for (int i = 0; i < 3; i++) {
            dp[0][i] = cost[0][i];
        }

        // 对于一般dp[i][j]
        // 其值等于当前行的前一行与dp[i][j]不同列(即0到j-1,或j+1到2列)的最小累计花费加上当前位置的所需花费
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < 3; j++) {
                dp[i][j] = getMinCostOfLastRow(dp[i - 1], j) + cost[i][j];
            }
        }

        int res = Integer.MAX_VALUE;
        for (int i = 0; i < 3; i++) {
            res = dp[n - 1][i] < res ? dp[n - 1][i] : res;
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int cases = Integer.parseInt(sc.nextLine());
        while (cases-- > 0) {
            int numOfSalers = Integer.parseInt(sc.nextLine());
            int[][] cost = new int[numOfSalers][3];

            for (int i = 0; i < numOfSalers; i++) {
                cost[i] = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            }

            int res = getMinCost(cost, numOfSalers);
            System.out.println(res);
        }
        sc.close();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值