• I


    貌似就是个裸的最小生成树啊
    *******************************************************************************
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<algorithm>
    using namespace std;

    const int maxn = 105;
    const int oo = 0xfffffff;

    int G[maxn][maxn];

    int prim(int N)
    {
        int i, j, dist[maxn]={0}, use[maxn]={01};

        for(i=2; i<=N; i++)
            dist[i] = G[1][i];
        for(i=1; i<N; i++)
        {
            int k = 1, Min = oo;

            for(j=1; j<=N; j++)
            {
                if(!use[j] && Min > dist[j])
                    Min = dist[j], k = j;
            }

            use[k] = true;

            for(j=1; j<=N; j++)
                if(!use[j])dist[j] = min(dist[j], G[k][j]);
        }

        int ans = 0;

        for(i=1; i<=N; i++)
            ans += dist[i];

        return ans;
    }

    int main()
    {
        int N;

        while(scanf("%d", &N) != EOF)
        {
            for(int i=1; i<=N; i++)
            for(int j=1; j<=N; j++)
            {
                scanf("%d", &G[i][j]);
            }

            int ans = prim(N);

            printf("%d ", ans);
        }

        return 0;

    } 

  • 相关阅读:
    [LeetCode] 21. 合并两个有序链表
    [LeetCode] 5081. 步进数
    [LeetCode] 104. 二叉树的最大深度
    [LeetCode] 70. 爬楼梯
    Java开发手册1.5读书笔记
    [LeetCode] 509. 斐波那契数
    设计模式之UML类图以及类间关系
    [LeetCode] 50. Pow(x, n)
    [LeetCode] 206. 反转链表
    [LeetCode] 119. 杨辉三角 II
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4675551.html
Copyright © 2020-2023  润新知