The minimum spanning tree algorithm is simple
Just adding something new, there is some deeper understanding of the need for minimal spanning tree algorithms and the series of checks used.
Some complex problems of the method
#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; const int MAXN = 1005;struct point{int x; int y;} pp[maxn];struct edge{int S; int e; int Dist;} L[maxn*maxn];int n,q,m;int p[maxn];vector<int> g[10];int c[10];int distance_ (point A,point B) {return (a.x-b.x) * ( a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);} int cmp (Edge A,edge b) {return a.dist < b.dist;} int find_ (int x) {return p[x]==x?x:p[x]=find_ (p[x]);} BOOL Merge_ (int a,int b) {int x=find_ (a); int Y=find_ (b); if (x==y) return false; P[x]=y; return true;} int Kruskal () {int ans=0; int num=0; for (int i=0;i<m&&num<n-1;i++) {if (Merge_ (L[I].S,L[I].E)) {num++; Ans+=l[i].dist; }} return ans; void Solve () {for (int i=0;i<=n;i++) p[i]=i; int ans = Kruskal (); for (int s=1;s< (1<<Q); s++) {int cost=0; for (int tt=0;tt<=n;tt++) P[tt]=tt; for (int j=0;j<q;j++) {if (!) ( (S>>J) &1) continue; COST+=C[J]; for (int k=0;k<g[j].size (); k++) {Merge_ (g[j][k],g[j][0]); }} ans=min (Ans,cost+kruskal ()); } printf ("%d\n", ans);} int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&q); for (int i=0;i<10;i++) g[i].clear (); for (int i=0;i<q;i++) {int cnt; scanf ("%d%d", &cnt,&c[i]); int A; for (int j=0;j<cnt;j++) {scanf ("%d", &a); G[i].push_back (a); }} for (int i=1;i<=n;i++) {scanf ("%d%d", &pp[i].x,&pp[i].y); } m=0; for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {l[m].s=i; L[m].e=j; L[m++].dist=distance_ (pp[i],pp[J]); }} sort (l,l+m,cmp); Solve (); if (t) printf ("\ n"); } return 0;}
Given the need to enumerate the sub-algorithms using several options.
The workaround in the above. It uses a subset of the help of the binary counting method. A subset of the enumeration algorithm applies only to a relatively small set of elements.
Taking the structure above represents the method edge of the method, rather than an open array. I think the code with the structure can be more readable.
UVA 1151-buy or build POJ 2784 Buy or build (minimum spanning tree)