Minimum spanning tree water problem, first by the smallest spanning tree, the answer is finally added to the minimum cost of wiring from the I building to the external power supply facilities.
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intMAXN = -;structabc{intUU, VV, CC;} node[maxn* (MAXN-1) /2+Ten];intuu[maxn* (MAXN-1) /2+Ten];intvv[maxn* (MAXN-1) /2+Ten];intcc[maxn* (MAXN-1) /2+Ten];intFF[MAXN], FATHER[MAXN];BOOLcmpConstAbc&a,ConstABC&B) {returna.cc <b.cc;}intFindintx) { if(x = father[x]) father[x] =find (father[x]); returnfather[x];}intMain () {intT; scanf ("%d", &T); while(t--) { intI, n, M; scanf ("%d%d", &n, &m); for(i =0; I < m; i++) scanf ("%d%d%d", &uu[i], &vv[i], &Cc[i]); for(i =0; I < n; i++) scanf ("%d", &Ff[i]); for(i =0; I <= N; i++) Father[i] =i; for(i =0; I < m; i++) {Node[i].uu=Uu[i]; NODE[I].VV=Vv[i]; node[i].cc=Cc[i]; } Sort (node, node+m, CMP); Sort (FF, FF+N); intAnss =0; for(i =0; I < m; i++) { intFu =find (NODE[I].UU); intFV =find (NODE[I].VV); if(Fu! =FV) {Father[fu]=FV; Anss= Anss +node[i].cc; }} printf ("%d\n", Anss + ff[0]); } return 0;}
Nyist OJ Topic 38 cabling Problems