Description
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 throughN. 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 consistsTTest cases. The number of test casesTIs given in the first line of the input file. Each test case consists of several lines. Two numbersV,E(0 ≤V,E≤ 50000) are given in the first line of each test case. On the next line,VPositive IntegersWiIndicating the weightsVNodes are given in one line. On the followingELines, each line contain three positive integersA,B,CIndicating the edge which is able to connect two nodesAAndB, And unit priceC.
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
22 11 11 2 157 7200 10 20 30 40 50 601 2 12 3 32 4 23 5 43 7 23 6 31 5 9
Sample output
151210
Build a Christmas tree to minimize the total cost. The specific rule is: the Christmas tree is an undirected tree. The Node numbered 1 is the root node. Each edge in the original graph has the edge weight (unit): the unit value of the material, each vertex also has a weight: the weight of the vertex. In the Spanning Tree, the cost of each vertex is the weight (weight) and, the total cost is the sum of some expenses in the Spanning Tree.
Train of Thought: the calculation method required by the derivation is the weight of each point * the shortest path to the root node. Only the shortest path can minimize the cost.
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <queue> # include <vector> # define INF 1ll <61 using namespace std; typedef long ll; const int maxn = 50005; struct edge {int from, to, DIST;}; struct heapnode {int D, U; bool operator <(const heapnode RHs) const {return D> RHS. d ;}}; struct Dijkstra {int n, m; // points and number of edges vector <edge> edges; // edge list vector <int> G [Ma XN]; // The edge number starting from each vertex (starting from 0) bool done [maxn]; // whether ll d [maxn] has been marked; // s the distance from each point to int P [maxn]; // The upper point in the shortest path, or the last side void Init (int n) {This-> N = N; For (INT I = 0; I <n; I ++) g [I]. clear (); edges. clear ();} void addedge (int from, int to, int Dist) {edges. push_back (edge) {from, to, DIST}); M = edges. size (); G [from]. push_back m-1);} void Dijkstra (INT s) {priority_queue