Big Christmas treetime limit: 3000 msmemory limit: 131072 kbthis problem will be judged on PKU. Original ID: 3013
64-bit integer Io format: % LLD Java class name: Main
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
Sourcepoj monthly -- 2006.09.29, Kim, Chan Min ([email protected]) Prev problem solving: seemingly minimal spanning tree, in fact, is to find the shortest path. How to prove it?
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
Assume that the weight of vertex 1 is 1,
1 to 2 have an edge and the weight is.
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 }
View code
Poj 3013 big Christmas tree