The main topic: Bessie to destroy the FJ plan, to make a maximum spanning tree.
Analysis: Just change a symbol of the minimum spanning tree algorithm. In addition, to determine whether the graph is connected
Code
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int MAXN = 20010;
struct edge{int S, E, W;
BOOL operator < (const Edge & E1) const {return w > e1.w;
}}E[MAXN];
int f[1010], n, m;
int ans;
int getfather (int x) {if (f[x] = = x) return x;
else return f[x] = Getfather (f[x]);
} int kruscal () {sort (e+1, e+m+1);
int cnt = n;
for (int i = 1; I <= n; i++) f[i] = i;
for (int i = 1; I <= m; i++) {int f1 = Getfather (E[I].S);
int F2 = Getfather (E[I].E);
if (f1! = F2) {F[F1] = F2;
Ans + = E[I].W;
cnt--; }} if (cnt = = 1) return true;
CNT representative; The number of the Unicom block else return false; } int main () {while (~scanf ("%d%d", &n, &m)) {for (int i = 1; I <= m; i++) scanf ("%d%d
%d ", &e[i].s, &E[I].E, &E[I].W);
if (kruscal ()) printf ("%d\n", ans); else PrinTF (" -1\n");
} return 0;
}