局域网——Prim Kruskal-Kruskal (如果有多颗生成树,生成最小生成森林)

#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;
}

上一篇:Linux网络:UDP socket - 简单聊天室-聊天通信架构


下一篇:基于大型语言模型的智能网页抓取