Test instructions: Give the coordinates of n points, now need to let this n points connected, you can directly between the point and point, the cost of two points between Euclidean distance squared, you can also choose a package, the points contained in the package is interconnected to ask the least cost
First of all, to Kruskal algorithm, the added edge is already the best, so when the package is selected, the previously discarded edges will not enter the minimum spanning tree
You can then find the smallest spanning tree of the original image and save the n-1 edge into the smallest spanning tree.
Then enumerate the selected packages, and then the minimum spanning tree, where the binary enumeration is used to last maintain a minimum value.
Although the idea is understood, but the code is not written at all, looking at the standard writing, and finally changed so long-sad----------
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A - #defineforeach (I,c) for (__typeof (C.begin ()) i = C.begin (); I! = C.end (); ++i) - thetypedefLong LongLL; - Const intINF = (1<< -)-1; - Const intMod=1000000007; - Const intmaxn=1005; + Const intmaxq=8; - + intN; A intX[MAXN],Y[MAXN],COST[MAXN]; atvector<int>SUBN[MAXN]; - - intP[MAXN]; - intFindintx) {returnP[x]!=x? p[x]=find (P[x]): x;} - - structedge{ in intu,v,d; -EdgeintUintVintd): U (U), V (v), D (d) {} to BOOL operator< (Constedge& RHS)Const{ + returnd<RHS.D;} - }; the * intMstintCntConstvector<edge>& e,vector<edge>&used) { $ if(cnt==1)return 0;Panax Notoginseng intm=e.size (); - intans=0; the used.clear (); + for(intI=0; i<m;i++){ A intU=find (e[i].u), v=find (E[I].V); the intD=e[i].d; + if(u!=v) { -p[u]=v; $ans+=D; $ Used.push_back (E[i]); - if(--cnt==1) Break; - } the } - returnans;Wuyi } the - intMain () { Wu //freopen ("In.txt", "R", stdin); - //freopen ("Outttttttt.txt", "w", stdout); About intt,q; $scanf"%d",&T); - while(t--){ -scanf"%d%d",&n,&q); - for(intI=0; i<q;i++){ A intCNT; +scanf"%d%d",&cnt,&cost[i]); the subn[i].clear (); - while(cnt--){ $ intu; thescanf"%d",&u); theSubn[i].push_back (U1); the } the } - in for(intI=0; i<n;i++) scanf ("%d%d",&x[i],&y[i]); the theVector<edge>E,need; About the for(intI=0; i<n;i++) the for(intj=i+1; j<n;j++){ the intC= (X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (y[i]-y[j]); + E.push_back (Edge (I,j,c)); - } the Bayi for(intI=0; i<n;i++) p[i]=i; the sort (E.begin (), E.end ()); the - intans=MST (n,e,need); - the for(intmask=0;mask< (1<<Q); mask++){ the the for(intI=0; i<n;i++) p[i]=i; the intCnt=n,c=0; - the for(intI=0; i<q;i++)if(Mask & (1<<i)) { thec+=Cost[i]; the for(intj=1; J<subn[i].size (); j + +){94 intU=find (Subn[i][j]), V=find (subn[i][0]); the if(U!=V) {p[u]=v;cnt--;} the } the }98Vector<edge>dummy; AboutAns=min (ans,c+MST (Cnt,need,dummy)); - }101printf"%d\n", ans);102 if(T) printf ("\ n"); 103 }104 return 0; the}View Code
UVa 1151 Buy or build "minimum spanning tree"