编程介的小学生 2017-04-06 16:38 采纳率: 20.5%
浏览 813
已采纳

Apple Transportation

There's a big apple tree in the forest. In the tree there are N nodes (numbered from 0 to N - 1), and the nodes are connected by branches. On each node of the tree, there is a squirrel. In the autumn, some apples will grow on the nodes. After all apples are ripe, each squirrel will collect all the apples of their own node and store them. For the demand to be harmonic, they decide to redistribute the apples to minimize the variance (please refer to the hint) of apples in all nodes. Obviously, an apple cannot be divided into several parts. To reach this goal, some transportation should be taken. The cost of transporting x apples from node u to node v is x * distance (node u, node v). Now given the current amount of apples of each node and the structure of the apple tree, you should help the squirrels to find the minimal cost to redistribute the apples.

Input

Input consists of multiple test cases (less than 80 cases)!

For each test case, the first line contains an integer N (1 <= N <= 100), which is the number of the nodes in the tree.

The following line contains N integers a0,a1,...,aN-1 (0 <= ai <= 10000), representing the amount of the i-th node's apples.

The following N - 1 lines each contain three integers u, v, c (0 <= u,v <= N - 1, 0 <= c <= 1000), which means node u and node v are connected by a branch, the length of the branch is c.

There is a blank line between consecutive cases.

Output

For each case output the minimal total transportation cost. The minimal cost is guaranteed to be less than 231.

Sample Input

3
1 2 3
0 1 1
0 2 1

3
1 3 3
0 1 3
0 2 4

2
1 2
0 1 1
Sample Output

1
3
0

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-20 15:42
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题