Big Christmas Tree
Test instructions: Enter the graph of the V node and the E-bar (0≤ v, e ≤50000), enter the weights for each node, and then enter the endpoints and weights for each edge in line E;
Ask if you can find a tree so that the Benquan in the tree is multiplied by the sum of the values of the descendants of the descendant nodes below the edge to the minimum; (tree with 1 as the root node)
Sample Input
1
200 10 20 30 40 50 601 2 12 3 32 4 23 5 43 7 23 6 31 5 9 (Erase)
Sample Output
1210
Analysis: If the calculation of the minimum product is really according to the topic, for each side to accumulate the edge of the node under the sum of the weights; but this is directly fooled. In turn, the equivalent of each node is calculated several times . (several times refers to the Benquan of the root node) so that the direct Djistra run single source the shortest possible, using the priority queue, directly determine the minimum path of the point is long, so you can calculate the ans in Djistra, but also to see whether it is connected graph;
PS: Note the special sentence v = 0; cnt = 1 > V;
Djistra + priority_queue
//Accepted 2804K 141MS#include <cstdio>#include<cstring>#include<utility>#include<queue>#include<vector>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineINF 1ll<<40Template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}Const intN =50050; typedef __int64 LL;TYPEDEF Pair<__int64,int> Lli;//Distance, number#defineA First#defineB Secondpriority_queue<lli, vector<lli>, greater<lli> >Q;intV,val[n];inthead[n<<1],tot;structedge{intTo,w,next;} E[n<<1];voidInsintAintBintW =0) {e[++tot]. Next =Head[a]; E[tot].to=b; E[TOT].W=W; Head[a]=tot;} ll D[n];BOOLvis[n];ll Djistra () {ll ans=0, cnt =0; d[1] =0; Q.push (lli{d[1],1}); while(!Q.empty ()) {Lli T=Q.top (); Q.pop (); intU =t.b; if(Vis[u])Continue; CNT++;vis[u] =true; Ans+ = d[u]*Val[u]; for(intid = Head[u];id;id =E[id]. Next) {intv = e[id].to,cost =E[ID].W; if(D[v] > D[u] +Cost ) {D[v]= D[u] +Cost ; Q.push (Lli{d[v],v}); } } } if(CNT < V)return-1; returnans;}intMain () {intT,e,kase =1; Read1 (T); while(t--) {read2 (v,e); REP1 (i,1, V) read1 (Val[i]), D[i]= Inf,vis[i] =false; MS0 (head); Tot=0; Rep0 (i,0, E) { inta,b,w; Read3 (A,B,W); Ins (a,b,w); ins (b,a,w); } if(v <=1) out(0); Else{ while(!q.empty ()) Q.pop (); LL ret=Djistra (); if(ret = =-1) printf ("No Answer"); Else out(ret); } puts (""); } return 0;}
View Code
POJ 3013 Big Christmas Tree