【每日一题】2646. 最小化旅行的价格总和-2023.12.5

题目:

2646. 最小化旅行的价格总和

现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 之间存在一条边。

每个节点都关联一个价格。给你一个整数数组 price ,其中 price[i] 是第 i 个节点的价格。

给定路径的 价格总和 是该路径上所有节点的价格之和。

另给你一个二维整数数组 trips ,其中 trips[i] = [starti, endi] 表示您从节点 starti 开始第 i 次旅行,并通过任何你喜欢的路径前往节点 endi 。

在执行第一次旅行之前,你可以选择一些 非相邻节点 并将价格减半。

返回执行所有旅行的最小价格总和。

示例 1:

输入:n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
输出:23
解释:
上图表示将节点 2 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0 、2 和 3 并使其价格减半后的树。
第 1 次旅行,选择路径 [0,1,3] 。路径的价格总和为 1 + 2 + 3 = 6 。
第 2 次旅行,选择路径 [2,1] 。路径的价格总和为 2 + 5 = 7 。
第 3 次旅行,选择路径 [2,1,3] 。路径的价格总和为 5 + 2 + 3 = 10 。
所有旅行的价格总和为 6 + 7 + 10 = 23 。可以证明,23 是可以实现的最小答案。

示例 2:

输入:n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
输出:1
解释:
上图表示将节点 0 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0 并使其价格减半后的树。 
第 1 次旅行,选择路径 [0] 。路径的价格总和为 1 。 
所有旅行的价格总和为 1 。可以证明,1 是可以实现的最小答案。

提示:

  • 1 <= n <= 50
  • edges.length == n - 1
  • 0 <= ai, bi <= n - 1
  • edges 表示一棵有效的树
  • price.length == n
  • price[i] 是一个偶数
  • 1 <= price[i] <= 1000
  • 1 <= trips.length <= 100
  • 0 <= starti, endi <= n - 1

解答:

代码:

class Solution {
    public int minimumTotalPrice(int n, int[][] edges, int[] price, int[][] trips) {
        List<Integer>[] next=new List[n];
        for(int i=0;i<n;i++){
            next[i]=new ArrayList<Integer>();
        }
        for(int[] edge:edges){
            next[edge[0]].add(edge[1]);
            next[edge[1]].add(edge[0]);
        }
        int[] count=new int[n];
        for(int[] trip:trips){
            dfs(trip[0],-1,trip[1],next,count);
        }
        int[] pair=dp(0,-1,price,next,count);
        return Math.min(pair[0],pair[1]);
    }
    public boolean dfs(int node,int parent,int end,List<Integer>[] next,int[] count){
        if(node==end){
            count[node]++;
            return true;
        }
        for(int child:next[node]){
            if(child==parent){
                continue;
            }
            if(dfs(child,node,end,next,count)){
                count[node]++;
                return true;
            }
        }
        return false;
    }
    public int[] dp(int node,int parent,int[] price,List<Integer>[] next,int[] count){
        int[] res={price[node]*count[node],price[node]*count[node]/2};
        for(int child:next[node]){
            if(child==parent){
                continue;
            }
            int[] pair=dp(child,node,price,next,count);
            res[0]+=Math.min(pair[0],pair[1]);//node没有减半,因此可以取子树的两种情况的最小值
            res[1]+=pair[0];//node减半,只能取子树没有减半的情况
        }
        return res;
    }
}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
30000.00 20000.00 20000.00 1200.00 1800.00 2400.00 1800.00 1800.00 1200.00 2400.00 1800.00 2200.00 2400.00 1800.00 1800.00 1200.00 2400.00 1200.00 2200.00 2400.00 2400.00 1800.00 1800.00 1800.00 2400.00 0.00 2400.00 2400.00 2400.00 2400.00 2400.00 2400.00 2400.00 -45900.00 -4200.00 -1400.00 -18700.00 -30000.00 -6800.00 -18600.00 -4000.00 -2600.00 -24200.00 -1400.00 -3000.00 -4200.00 -2600.00 -16000.00 -2600.00 -1300.00 -1300.00 -4300.00 -4200.00 -56300.00 -6800.00 -1300.00 -4100.00 -2600.00 -2700.00 -27900.00 -40800.00 -61700.00 -36600.00 -36600.00 -42800.00 -29600.00 -32400.00 -49000.00 -49400.00 -25300.00 -5300.00 -3900.00 -2600.00 -8200.00 -2600.00 -1300.00 -3900.00 -6700.00 -11900.00 -2600.00 -1300.00 -2700.00 -1300.00 -4200.00 -1400.00 -3900.00 -2600.00 -1400.00 -7900.00 -4200.00 -2900.00 -2600.00 -23100.00 -2600.00 -2700.00 -1300.00 -11000.00 -4200.00 -8400.00 -2600.00 -10600.00 -1300.00 -5300.00 -10700.00 -3900.00 -9200.00 -5500.00 -14100.00 -35600.00 -42100.00 -14600.00 -2700.00 -6800.00 -16600.00 -1300.00 -1300.00 -4000.00 -2900.00 -13300.00 -2600.00 -6600.00 -2600.00 -2700.00 -3900.00 -1300.00 -7800.00 -2600.00 -17900.00 -17400.00 -8100.00 -16000.00 -17500.00 -14700.00 -9400.00 -5200.00 -2600.00 -2700.00 -1300.00 -2600.00 -1300.00 -4000.00 -5900.00 -13100.00 -22700.00 -1300.00 -3900.00 -1300.00 -5500.00 -4100.00 -26700.00 -1400.00 -4000.00 -1300.00 -2700.00 -5500.00 -1400.00 -12200.00 -15000.00 -1300.00 -4000.00 -1300.00 -1500.00 -3900.00 -334300.00 -2600.00 -2600.00 -39500.00 -184900.00 -13400.00 -5400.00 -3900.00 3000.00求总和等于多少?
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩軒轩儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值