1 条题解

  • 1
    @ 2024-7-25 13:42:36

    参考答案:

    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    const int M = 200010, N = 1010;
    int g[N][N];
    int st[N], dis[N];
    int n, m, res;
    
    bool prim()
    {
        memset(dis, 0x3f, sizeof(dis));
    
        for (int i = 0; i < n; ++i)
        {
            int t = -1;
            for (int j = 1; j <= n; ++j)
                if (!st[j] && (t == -1 || dis[j] < dis[t]))
                    t = j;
            if (i && dis[t] == 0x3f3f3f3f) return false;
            if (i) res += dis[t];
            for (int j = 1; j <= n; ++j)
                dis[j] = min(dis[j], g[t][j]);
            st[t] = 1;
        }
    
        return true;
    }
    int main()
    {
        cin >> n >> m;
    
        memset(g, 0x3f, sizeof(g));
    
        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);
        }
    
        bool t = prim();
    
        if (t) cout << res;
        else cout << "Not exist";
    
        return 0;
    }
    
    • 1

    信息

    ID
    102
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者