Test instructions: The minimum cost of finding a graph as a connected graph
Positive solution: Kruskal minimum spanning tree algorithm
It is also a template water problem, the minimum spanning tree direct water.
It is made by jump~ #include <iostream> #include <vector> #include <algorithm> #include <cstring > #include <cstdio> #include <cmath>using namespace Std;int n,m,total,maxl;int father[311]; struct node{int u,to,w; }JUMP[300011]; int Getint () {int w=0,q=0; Char C=getchar (); while ((c< ' 0 ' | | c> ' 9 ') && c!= '-') C=getchar (); if (c== '-') q=1, C=getchar (); while (c>= ' 0 ' && c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); Return q? -w:w;} BOOL CMP (node A,node b) {return a.w<b.w;} int find (int x) {if (father[x]!=x) Father[x]=find (father[x]); return father[x]; } void hebing (int x,int y) {father[y]=x; } int main () {n=getint (); M=getint (); for (int i=1;i<=m;i++) {jump[i].u=getint (); Jump[i].to=getint (); Jump[i].w=getint (); } for (int i=1;i<=n;i++) father[i]=i; Sort (jump+1,jump+m+1,cmp); for (int i=1;i<=m;i++) {int R1=find (JUMP[I].U), r2=Find (jump[i].to); if (R1!=R2) {hebing (R1,R2); if (JUMP[I].W>MAXL) MAXL=JUMP[I].W; total++; }} printf ("%d%d", TOTAL,MAXL); return 0;}
(SCOI2005) BZOJ1083 busy city