Test instructions: To enable interoperability between n points, it is necessary to make the two-point direct interoperability requires the right of the square of Euclidean distance between them (note that there is no need to prescribe), which means that every two points can be interconnected. Then q A package can be selected, once selected these packages, they are included in the point of automatic connection, the need to do is not connected to the can, q<=8. Can buy more. The cost of minimizing the spanning tree.
Ideas: Unlike ordinary MST is more than the package, but also can buy more. Each package has two possibilities of buying or not buying, then there are 28 possibilities, that is, 256 kinds.
If you do not buy a package, at least 1 times the MST is determined, and this complexity is already O (n*n). Also have to consider which meal sets can be used to buy cheaper, then the poor lifting of the 256 combinations, each combination to the MST, but no longer need O (n*n), only need to use the first generation of the tree picked out the edge can be, those are MST, do not pick who they choose.
This is done by connecting all the points within the package (and checking the set), and then using the MST side to kruscal (remember to add the package fee). For each combination, you can find the result.
In particular, it is important to note that there are 1 blank lines between each of the two output results, and no more blank lines at the end, otherwise an error occurs.
1#include <bits/stdc++.h>2 #defineLL Long Long3 using namespacestd;4 Const intn= ++5;5 Const intinf=0x7f7f7f7f;6vector<int> vect[Ten];7vector< pair<int,int> >cor, E, tree;8 intT, R, N, Q, A, B;9 intcost[Ten], Pre[n], g[n][n];;Ten One intCMP (pair<int,int> a,pair<int,int> B) {returnG[a.first][a.second]<g[b.first][b.second]?true:false;}//Sort by distance A intDis (pair<int,int> a,pair<int,int> B) {return(A.first-b.first) * (A.first-b.first) + (a.second-b.second) * (A.second-b.second);}//no need to prescribe . - - intFindintx) {returnPre[x]==x? X:pre[x]=find (Pre[x]);}//Check the voidJointintAintb) {A=find (a), b=find (b);if(a!=b) pre[a]=b;}//and - - - +LL kruscal ()//take the tree edge out of the spanning tree - { + for(intI=1; i<=n; i++) pre[i]=i; A intCnt=0; atLL sum=0; - for(intI=0; I<e.size (); i++) - { - intA=E[i].first; - intb=E[i].second; - if(Find (a)! =find (b)) in { -cnt++; toTree.push_back (E[i]);//Favorite Edges +SUM+=G[A][B];//Statistic Weight Value -Joint (A, b);//A and B are points the if(cnt>=n-1)returnsum; * } $ }Panax Notoginseng returnsum; - } the + ALL kruscal_2 ()//with a set menu the { +LL sum=0; - for(intI=0; I<tree.size (); i++) $ { $ intA=Tree[i].first; - intb=Tree[i].second; - if(Find (a)! =find (b)) the { -sum+=G[a][b];Wuyi Joint (A, b); the } - } Wu returnsum; - } About $ LL cal () - { - sort (E.begin (), E.end (), CMP); - tree.clear (); ALL ans=kruscal ();//first spanning tree, picking out useful edges + intChoice=1; the while(q--) choice+=choice; - for(intI=1; i<choice; i++) $ { the for(intj=1; j<=n; J + +) pre[j]=J; the intTmp=i, cnt=1; theLL sum=0; the while(TMP)//first categorize the pre-purchase package - { in if((tmp&1)==1)//The first CNT package is for you. the { thesum+=cost[cnt]; About for(intj=1; J<vect[cnt].size (); J + +) Joint (vect[cnt][j-1],vect[cnt][j]); the } thetmp>>=1; thecnt++; + } -Ans=min (ans, sum+kruscal_2 ());//Build Tree Again the }Bayi returnans; the } the - intMain () - { theFreopen ("Input.txt","R", stdin); theCin>>T; the while(t--) the { -Cin>>n>>Q; the for(intI=1; i<=q; i++)//each package the { thescanf"%d%d",&a,&cost[i]);94 vect[i].clear (); the while(a--) the { thescanf"%d",&R);98 Vect[i].push_back (r); About } - }101 cor.clear ();102 for(intI=0; i<n; i++)103 {104scanf"%d%d",&a,&b); theCor.push_back (Make_pair (A, b));//coordinates for each point106 }107 108Memset (G,0,sizeof(g));109 e.clear (); the for(intI=1; i<=n; i++)//Calculate the distance111 { the for(intj=i+1; j<=n; J + +)113 { theg[i][j]=g[j][i]= Dis (cor[i-1],cor[j-1]); the E.push_back (Make_pair (i,j)); the }117 }118Cout<<cal () <<Endl;119 if(t) printf ("\ n"); - }121 return 0;122}
AC Code
UVA 1151 Buy or build (MST minimum spanning tree, kruscal, morph)