题解
这是一道裸的最小生成树题,拿来练手,题目就不放了
个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点
Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一颗树
AC代码:
Prim算法:
#include #include //EOF,NULL#include //memset#include //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc#include //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))#include //fill,reverse,next_permutation,__gcd,#include #include #include #include #include #include #include //setw(set_min_width),setfill(char),setprecision(n),fixed,#include #include
Kruskal算法:
#include #include //EOF,NULL#include //memset#include //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc#include //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))#include //fill,reverse,next_permutation,__gcd,#include #include #include #include #include #include #include //setw(set_min_width),setfill(char),setprecision(n),fixed,#include #include