题目
Prim (生成一颗包含起点的最小生成树,所以要多次调用)
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int inf = 0x3f3f3f3f;
int n, m;
int g[N][N], dis[N];
bool p[N], vis[N];
int prim (int u)
{
memset(dis, 0x3f, sizeof dis); dis[u] = 0;
int sum = 0;
for(int i = 0 ; i < n ; i ++ )
{
int t = -1;
for(int j = 1 ; j <= n ; j ++ )
if(!p[j] && (t == -1 || dis[t] > dis[j]))
t = j;
if(i && dis[t] == inf) return sum;
p[t] = 1;
if(i) sum += dis[t]; // 第一个点为根节点没有边权
vis[t] = 1;
for(int j = 1 ; j <= n ; j ++ )
if(!p[j] && dis[j] > g[t][j]) dis[j] = g[t][j];
}
return sum;
}
int main ()
{
int ans = 0;
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++ )
for(int j = 1 ; j <= n ; j ++ )
if(i == j) g[i][j] = 0;
else g[i][j] = inf;
for(int i = 1 ; i <= m ; i ++ )
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
ans += min(g[a][b], c);
}
int t = 0;
for(int i = 1 ; i <= n ; i ++ )
if(!vis[i]) t += prim(i);
cout << ans - t << endl;
return 0;
}
Kruskal (如果有多颗生成树,生成最小生成森林)
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int M = 210;
struct edge{
int a;
int b;
int c;
bool operator < (const edge& v)
{
return c < v.c;
}
} e[M];
int p[N];
int n, idx, m;
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int kruskal()
{
int retv = 0;
for(int i = 1; i <= n; i++)
p[i] = i;
sort(e+1,e+m+1);
for(int i = 1; i <= m; i++)
{
int a = e[i].a, b = e[i].b, c = e[i].c;
a = find(a), b = find(b);
if(a != b)
{
p[a] = b;
retv += c;
}
}
return retv;
}
int main()
{
cin >> n >> m;
int sum = 0;
for(int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
e[++idx] = {a, b, c};
sum += c;
}
int t = kruskal();
cout << sum - t;
}