zoukankan      html  css  js  c++  java
  • 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;

    } 

  • 相关阅读:
    排序小结
    递推
    基准线
    毫无思绪
    计蒜客A
    尼克的任务
    售货员的难题
    Renting Boats
    工业物联网实践指南----专注生产制造活动
    阿里云单域名免费SSL证书安装
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4675551.html
Copyright © 2011-2022 走看看