• POJ 1258 AgriNet (最小生成树入门题目)


    原题链接:http://poj.org/problem?id=1258

    周四上了节数据结构终于知道何为最小生成树,晚上回寝,遂得此题!

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 29613   Accepted: 11750

    Description

    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

    题意:

                给你N*N矩阵,表示N个村庄之间的距离。FJ要把N个村庄全都连接起来,求连接的最短距离。(即传说中的最小生成树)

               样例分析:

                                连接方式:1-----------2-----------3----------4

                                距离:                 4      +       8     +        16         =                  24       

    算法:图论之最小生成树问题。。。

               最小生成树算法思想:

                                     假设N=(V,E) 是连通网,TE是N上最小生成树中边的集合,算法从U={vk},TE={ }开始(即从vk出发求最小生成树,vk∈V)。

                                     重复执行下述操作:
                                     在所有的边(vi,vj)∈E (vi∈U,vj∈V-U)中寻找一条权值最小的边(vi,vj)将其添加到TE中(或打印之),同时把vj添 加到集合U 中 。
                                     反复执行上述操作n-1次(或所有顶点全部加入U时为止)。                

              通俗的说:1、有一个图N=(V,E),设置一个空集合U,设置路长ans=0。

                                  2、就是先找一个点v1,将v1添加到集合U,再找出和它距离最近的点v2,再把v2添加到集合U,ans加上两点间的距离。

                                  3、再到点集合V-U (属于V,但是不属于U的点)中找出一个离集合U最近的点。将找到的点添加到集合U,ans加上这个最短的距离。

                                  4、如此循环第三步,直到所有的点全到U中。

            关于这个动态的PPT,见我传的老师课件:

    //AC
    #include<stdio.h>
    #include<string.h>
    int map[105][105];
    int lowest[105];
    bool vis[105];
    int n;
    int min()//找到离已经连好了的村庄最短距离的村庄的标号
    {
        int i;
        int m=100000,k=-1;
        for(i=1;i<=n;i++)
        {
            if(!vis[i] && lowest[i]<m)
            {
                m=lowest[i];k=i;
            }
        }
        return k;
    }
    int main()
    {
        int i,j;
        int ans;
        while(scanf("%d",&n)!=EOF)
        {
            ans=0;
            memset(vis,false,sizeof(vis));
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    scanf("%d",&map[i][j]);
                }
            }
            for(i=1;i<=n;i++) lowest[i]=map[1][i];
            vis[1]=true;
            for(i=1;i<n;i++)//找出n-1条最短边
            {
                int k=min();
                vis[k]=true;ans+=lowest[k];//找到一个马上标记,并且加上权值
                for(j=1;j<=n;j++)//找到一个新的点,马上更新最小距离。
                {
                    if(map[k][j]<lowest[j])
                    {
                        lowest[j]=map[k][j];
                    }
                }
            }
            printf("%d\n",ans);
        }
        return 0;
    }
      
    
                               
    
                                 
  • 相关阅读:
    Posix线程编程指南(3) 线程同步
    Posix线程编程指南(1) 线程创建与取消
    #pragma once
    pycharm中import动态链接库pyd有错误
    vs2013 + python3.52 + boost1.61, 编译C++库失败
    VS2013下的64位与32位程序配置
    首篇
    B . Medal Ranking -UCF Local Programming Contest 2015
    A . Find the Twins -UCF Local Programming Contest 2015
    Restricted RPS CodeForces
  • 原文地址:https://www.cnblogs.com/freezhan/p/2776470.html
Copyright © 2020-2023  润新知