Test Instructions: give a graph, each node has the right value, each edge also has the cost. A tree is required to make the total cost minimal. The cost of each side of the tree is defined as the child node weights and x this side cost.
Practice: analysis shows that the final answer is the weight of all nodes x to the distance of the root node. It is possible to know the minimum cost when the distance is shortest.
So with the Dijkstra+ Priority queue optimization can be done. This problem has some card time. Finally, it is also important to use long long, which n=0 and n=1.
1 /*--------------------------------------------------------------------------------------*/2 //Helica ' s header3 //Second Edition4 //2015.11.75 //6#include <algorithm>7#include <iostream>8#include <cstring>9#include <ctype.h>Ten#include <cstdlib> One#include <cstdio> A#include <vector> -#include <string> -#include <queue> the#include <stack> -#include <cmath> -#include <Set> -#include <map> + - //Debug function for a n*m array + #defineDebug_map (n,m,g) printf ("\ n"); for (int i=0;i< (N); i++) A{ for(intj=0;j< (M); J + +){ atprintf"%d", G[i][j]);} printf"\ n");} - //Debug function for int,float,double,etc. - #defineDebug_var (X) cout<< #X "=" <<X<<endl; - /*--------------------------------------------------------------------------------------*/ - using namespacestd; - in Const intMAXN = 5e4+Ten; - Const Long LongINF =0x3f3f3f3f3f3f3f3f; to intn,m,t,s; + - structQnode the { * intv,c; $Qnode (int_v=0,int_c=0): V (_v), C (_c) {}Panax Notoginseng BOOL operator< (ConstQnode &r)Const -{returnC>r.c;} the }; + A structEdge the { + intTo,next; - intCost ; $}edge[4*MAXN]; $ - intHEAD[MAXN],TOL,WEIGHT[MAXN]; - Long LongDIS[MAXN]; the BOOLVIS[MAXN]; -Priority_queue<qnode>que;Wuyi the voidDijkstra (intNintstart) - { Wumemset (Vis,false,sizeofvis); - for(intI=1; i<=n;i++) Dis[i] =INF; AboutDis[start] =0; $ while(!que.empty ()) Que.pop (); - -Que.push (Qnode (Start,0)); - Qnode cur; A while(!que.empty ()) + { theCur =que.top (); - Que.pop (); $ intU =cur.v; the if(Vis[u])Continue; theVis[u] =true; the the for(intI=head[u];~i;i=edge[i].next) - { in intv =edge[i].to; the intCost =Edge[i].cost; the //printf ("u:%d v:%d cost:%d\n", u,v,cost); About if(!vis[v] && dis[v]>dis[u]+Cost ) the { theDIS[V] = dis[u]+Cost ; the Que.push (Qnode (v,dis[v)); + } - } the }Bayi } the the voidAdd_edge (intUintVintCost ) - { -Edge[tol].to =u; theEdge[tol].next =Head[v]; theEdge[tol].cost =Cost ; theHEAD[V] = tol++; the -Edge[tol].to =v; theEdge[tol].next =Head[u]; theEdge[tol].cost =Cost ; theHead[u] = tol++;94 } the the intMain () the {98scanf"%d",&T); About while(t--) - {101scanf"%d%d",&n,&M);102 for(intI=1; i<=n;i++) scanf ("%d",&weight[i]);103memset (head,-1,sizeofhead);104memset (Vis,false,sizeofvis); theTol =0;106 for(intI=0; i<m;i++)107 {108 inta,b,c;109scanf"%d%d%d",&a,&b,&c); the Add_edge (a,b,c);111 } the if(n==0|| n==1)113 { theprintf"0\n"); the Continue; the }117Dijkstra (N,1);118 119 Long LongAns =0; - BOOLFlag =true;121 for(intI=2; i<=n;i++)122 {123 if(Dis[i] = =INF)124 { theFlag =false;126 Break;127 } - Else129Ans + = weight[i]*Dis[i]; the }131 the if(!flag) printf ("No answer\n");133 Elseprintf"%lld\n", ans);134 }135}
Poj3013-big Christmas tree-Shortest circuit