poj 3013 Big Christmas Tree (最短路)

您所在的位置:网站首页 圣诞树重量 poj 3013 Big Christmas Tree (最短路)

poj 3013 Big Christmas Tree (最短路)

2024-06-15 08:24| 来源: 网络整理| 查看: 265

Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.

The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).

Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.

Input

The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c.

All numbers in input are less than 216.

Output

For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.

Sample Input

2 2 1 1 1 1 2 15 7 7 200 10 20 30 40 50 60 1 2 1 2 3 3 2 4 2 3 5 4 3 7 2 3 6 3 1 5 9

Sample Output

15 1210

题意:找出最小造价和,造价和等于(所有该子树上的节点的造价和)*(连接该子树与其父节点的边的造价和)。

#include #include #include #include #define M 50010 using namespace std; struct node { long long r,v,nextt; }a[2*M]; queueq; long long f[M],head[M],carry,vis[M]; long long dis[M]; void insert_pail(long long l,long long r,long long v) { carry++; a[carry].r=r; a[carry].v=v; a[carry].nextt=head[l]; head[l]=carry; } void spfa() { memset(dis,0x3f3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1]=0; q.push(1); while(!q.empty()) { long long cur=q.front(); q.pop(); long long k=head[cur]; while(k) { if(dis[a[k].r]>dis[cur]+(long long)a[k].v) { dis[a[k].r]=dis[cur]+(long long)a[k].v; if(!vis[a[k].r]) { q.push(a[k].r); } } k=a[k].nextt; } } } int main() { long long t,n,m,l,r,v; long long ans; scanf("%lld",&t); while(t--) { ans=0; memset(head,0,sizeof(head)); carry=0; scanf("%lld%lld",&n,&m); for(long long i=1;i


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3