zoukankan      html  css  js  c++  java
  • Agri-Net(最小生成树)

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
    The distance between any two farms will not exceed 100,000.

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

    已经将图存好,直接将它读入就好

    #include<stdio.h>
    #include<string.h>
    #define MAX 0x3f3f3f3f
    #include<iostream>
    using namespace std;
    int logo[1010];//用来标记0和1  表示这个点是否被选择过
    int map1[1010][1010];//邻接矩阵用来存储图的信息
    int dis[1010];//记录任意一点到这个点的最近距离
    int n;
    void prim()
    {
        int i,j,now;
        int sum=0;
        /*初始化*/
        for(i=1; i<=n; i++)
        {
            dis[i]=MAX;
            logo[i]=0;
        }
        /*选定1为起始点,初始化*/
        for(i=1; i<=n; i++)
        {
            dis[i]=map1[1][i];
        }
        dis[1]=0;
        logo[1]=1;
        /*循环找最小边,循环n-1次*/
        for(i=1; i<n; i++)
        {
            now=MAX;
            int min1=MAX;
            for(j=1; j<=n; j++)
            {
                if(logo[j]==0&&dis[j]<min1)
                {
                    now=j;
                    min1=dis[j];
                }
            }
            if(now==MAX)
                break;//防止不成图
            logo[now]=1;
            sum+=min1;
            for(j=1; j<=n; j++)//添入新点后更新最小距离
            {
                if(logo[j]==0&&dis[j]>map1[now][j])
                    dis[j]=map1[now][j];
            }
        }
        if(i<n)
            printf("?
    ");
        else
            printf("%d
    ",sum);
    }
    int main()
    {
            int m,i,j;
            while(~scanf("%d",&n))
            {
    
            m=n*(n-1)/2;//m是边数
            memset(map1,0x3f3f3f3f,sizeof(map1));//map是邻接矩阵存储图的信息
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                  scanf("%d",&map1[i][j]);
            }
            prim();
            }
            return 0;
    }
    
  • 相关阅读:
    概率面试题
    机器学习概率题总结(转载)
    筛素数以及判断数是否是素数
    腾讯2019正式批春笔试题
    推荐系统架构
    文本表示与匹配
    CTR预估经典模型总结
    spark运行原理
    leetcode 字符串动态规划总结
    无向图的邻接矩阵创建代码以及深度遍历广度遍历
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702786.html
Copyright © 2011-2022 走看看