//Prim算法 最小生成树
/*
把所有顶点分为 2 个集合 ,一个表示已经选中的顶点集合 另一个表示未选中的顶点集合
例如 a,b,c,d,e 五个顶点
1.任意选择一个顶点 放在 已经选中的顶点集合中 假如 选a
2.将a 与未选中顶点集合中 选择 一顶点 条件 权值最小的一个顶点 如何权值相同 则任意选择一个最小的
3.将上轮选中的顶点 重复2 直到所有顶点选择完毕
*/
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
#include <time.h>
#include <chrono>
using namespace std;
inline int read(int& x) {
char ch = getchar();
int f = 1; x = 0;
while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
return x * f;
}
static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();
class Union_find{
public:
Union_find(int n) :f(n, 0){ for (int i = 0; i < f.size(); i++)f[i] = i; }
void add(int x, int y){ int fa = getFa(x), fb = getFa(y); if (fa != fb)f[fa] = fb; }
int getFa(int x){ return x == f[x] ? x : f[x] = getFa(f[x]); }
private:
vector<int> f;
};
const int maxn = 107, maxm = 1e4 + 7;
int m[maxn][maxn],n,tot = 0,dis[maxn];
bool st[maxn];
struct edge{
int x, y, z;
void set(int _x, int _y, int _z){
x = _x; y = _y; z = _z;
}
bool operator<(const edge &eg){
return z < eg.z;
}
}e[maxm];
long long int Kruskal(){
sort(e, e + tot);
long long int ans = 0;
Union_find uf(n + 10);
for (int i = 0; i < tot; i++){
int x = e[i].x;
int y = e[i].y;
if (uf.getFa(x) != uf.getFa(y)){
uf.add(x, y);
ans += e[i].z;
}
}
return ans;
}
long long int Prim() {
// 如果图不连通返回 0x3f3f3f3f, 否则返回 res
memset(dis, 0x3f, sizeof dis);
dis[1] = 0;
long long int res = 0;
for (int i = 0; i < n; i++) {
int t = -1;
// 寻找离集合S最近的点
for (int j = 1; j <= n; j++) {
if (!st[j] && (t == -1 || dis[t] > dis[j]))
t = j;
}
// 不连通,无最小生成树
if (i && dis[t] == 0x3f3f3f3f) return 0x3f3f3f3f;
res += dis[t];
st[t] = true;
//最新所有点到 S 的距离,注意和 dijkstra 对比,不是路径权值和
for (int j = 1; j <= n; j++)
dis[j] = min(dis[j], m[t][j]);
}
return res;
}
int main(){
cin >> n;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cin >> m[i][j];
if (i != j){
e[tot++].set(i, j, m[i][j]);
}
}
}
cout << Kruskal() << endl;
cout << Prim() << endl;
return 0;
}
Kruskal算法
简要概过: 将所有边排序,从小到大依次取边, 如果添加这条边不会形成环,那么添加这条边.
环的判断:利用并查集(如果是一个大家庭里面的成员,那么不能再加了,we are family)
题目:洛谷 p3366
const int MAXSIZE = 2*1e5 + 11;
struct Edge
{
int from, to, wh;
Edge() :from(0), to(0), wh(0) {}
Edge(int f,int t,int w) :from(f), to(t), wh(w) {}
bool operator < (const Edge& other) {
return wh < other.wh;
}
};
Edge edges[MAXSIZE];
int n, m,nCount = 0;
int unit[MAXSIZE] = { 0 };
int GetFamily(int a) {
return a == unit[a] ? a : unit[a] = GetFamily(unit[a]);
}
void AddFamily(int a, int b) {
int fa = GetFamily(a);
int fb = GetFamily(b);
if (fa != fb) unit[fa] = unit[fb];
}
int Kruskal() {
int ans = 0;
for (int i = 1; i <= n; ++i) unit[i] = i;
sort(edges, edges + nCount);
for (int i = 0; i < m; ++i) {
int f = edges[i].from;
int t = edges[i].to;
if (GetFamily(f) != GetFamily(t)) {
AddFamily(f, t);
ans += edges[i].wh;
}
}
return ans;
}
int main()
{
cin >> n >> m;
int a, b, c;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
edges[nCount++] = Edge(a, b, c);
}
int ret = Kruskal();
cout << ret << endl;
return 0;
}