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;
    }
    
  • 相关阅读:
    操作系统 进程(下)
    操作系统 进程(上)
    进程的执行状态
    操作系统之内存与进程
    阿里巴巴一道笔试题
    ASP.NET 验证码 不同浏览器 不刷新问题
    Android常用控件之GridView使用BaseAdapter
    spoj 8222 Substrings (后缀自动机)
    【每天一个Linux命令】13. Linux中whereis命令的用法
    JSP 文件上传下载系列之二[Commons fileUpload]
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702786.html
Copyright © 2011-2022 走看看