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.
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.
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.
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
15 1210
1_a_2_b_3_c_4
2*a + (a+b)*3 + (a+b+c)*4
=> a*(2+3+4) + b*(3+4) + c*4
假设1号顶点的权重为1,
1到2有一条边,权为a。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #define LL long long 7 #define pli pair<LL,int> 8 #define INF 0x3f3f3f3f3f3f3f3fLL 9 using namespace std; 10 const int maxn = 50010; 11 struct arc{ 12 int to,next; 13 LL w; 14 arc(int x = 0,LL y = 0,int z = -1){ 15 to = x; 16 w = y; 17 next = z; 18 } 19 }; 20 LL w[maxn],d[maxn]; 21 int n,m,tot,head[maxn]; 22 arc e[maxn<<2]; 23 void add(int u,int v,LL w){ 24 e[tot] = arc(v,w,head[u]); 25 head[u] = tot++; 26 e[tot] = arc(u,w,head[v]); 27 head[v] = tot++; 28 } 29 priority_queue< pli,vector< pli > ,greater< pli > >q; 30 void Dijkstra(){ 31 while(!q.empty()) q.pop(); 32 for(int i = 1; i <= n; i++) d[i] = INF; 33 d[1] = 0; 34 q.push(make_pair(d[1],1)); 35 while(!q.empty()){ 36 pli p= q.top(); 37 q.pop(); 38 if(p.first > d[p.second]) continue; 39 for(int i = head[p.second]; ~i; i = e[i].next){ 40 if(d[e[i].to] > d[p.second] + e[i].w){ 41 d[e[i].to] = d[p.second] + e[i].w; 42 q.push(make_pair(d[e[i].to],e[i].to)); 43 } 44 } 45 } 46 } 47 int main(){ 48 int t,u,v,wt; 49 scanf("%d",&t); 50 while(t--){ 51 scanf("%d %d",&n,&m); 52 for(int i = 1; i <= n; i++) 53 scanf("%I64d",w+i); 54 memset(head,-1,sizeof(head)); 55 for(int i = tot = 0; i < m; i++){ 56 scanf("%d %d %d",&u,&v,&wt); 57 add(u,v,wt); 58 } 59 if(n == 0 || n == 1){ 60 puts("0"); 61 continue; 62 } 63 Dijkstra(); 64 bool flag = true; 65 LL MST = 0; 66 for(int i = 2; i <= n; i++){ 67 if(d[i] == INF) {flag = false;break;} 68 MST += d[i]*w[i]; 69 } 70 flag?printf("%I64d\n",MST):puts("No Answer"); 71 } 72 return 0; 73 }