Dynamic Programming - tree dp-1757 to collect diamonds

2020-04-08 08:05:15

Problem Description:

Garlic state-owned  n cities, numbered from  1 to  n, there are inter-city  n−1 roads, and to ensure that between any two cities are connected. Every city has a certain number of diamonds.

Jun garlic in the garlic country would like to collect diamonds. He was from the city  1 of departure, every day he drove to another city by road between the cities. When the garlic for the first time to a city, he can finish collecting all the diamonds of the city, if he came back again to this city, there is no brick it can be collected.

Jun garlic only  k days, would you please help calculate calculate how much garlic king of diamonds he could collect the most.

numRepresents the number of diamonds in each city, mpit represents all the way to the city.

Sample

Example 1:

输入: n=3,k=2,num=[3,8,3],mp=[[1,3],[3,2]]
输出: 14
解释:
他能得到所有的钻石

Sample 2:

输入: n=6,k=2,num=[5,9,8,4,9,2],mp=[[1,6],[6,2],[2,5],[5,3],[5,4]]
输出: 16
解释:
他可以得到第1、2、6个城市中的钻石

Precautions

  • 1 \ leq n \ leq100 1 n 1 0 0
  • 0 \ leq k \ leq200 0 k 2 0 0
  • num[i] \leq1000num[i]1000
  • Ensure that any two cities can reach directly or indirectly

Guess you like

Origin www.cnblogs.com/hyserendipity/p/12657676.html