zoukankan      html  css  js  c++  java
  • hdu 1233 还是畅通工程(最小生成树,基础)

    题目

    //也是标准的最小生成树啊,我就改一点点,不重新再打一遍增加熟练度了
    
    #define  _CRT_SECURE_NO_WARNINGS
    
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    #define M 110
    #define inf 999999999
    int mat[M][M];
    
    int prim(int n,int sta)
    {
        int i,j,sum=0,mark[M],dis[M];
        for(i=0;i<n;i++)
        {
            dis[i]=mat[sta][i];
            mark[i]=0;
        }
        mark[sta]=1;
        for(i=1;i<n;i++)
        {
            int minn=inf,flag;
            for(j=0;j<n;j++)
            {
                if(dis[j]<minn&&mark[j]==0)
                {
                    minn=dis[j];
                    flag=j;
                }
            }
            mark[flag]=1;
            sum+=dis[flag];
            for(j=0;j<n;j++)
            {
                if(dis[j]>mat[flag][j])
                    dis[j]=mat[flag][j];
            }
        }
        return sum;
    
    }
    
    
    
    
    int main()
    {
        int i,n,m,a,b,c;
        while(scanf("%d",&n),n)
        {
            m=n*(n-1)/2;
            for(i=0;i<m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                a--;b--;
                mat[a][b]=mat[b][a]=c;
            }
            printf("%d
    ",prim(n,0));
        }
        return 0;
    }
    View Code
    一道又一道,好高兴!
  • 相关阅读:
    Sublime 设置移动光标快捷键
    Reverse Linked List II
    Reverse Nodes in K-Group
    Sort Colors
    Swap Nodes in Pairs
    Intersection of Two Linked Lists
    Word Break
    Unique Binary Search Tree
    Maximal Square
    Happy Number
  • 原文地址:https://www.cnblogs.com/laiba2004/p/3521571.html
Copyright © 2011-2022 走看看