zoukankan      html  css  js  c++  java
  • acm专题---最小生成树

    kruscal(eloge):

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102

    Problem Description
    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
     
    Input
    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
     
    Output
    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 
     
    Sample Input
    3 0 990 692 990 0 179 692 179 0 1 1 2
     
    Sample Output
    179
    #include <iostream>
    using namespace std;
    #include <vector>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<map>
    #include<math.h>
    #include<iomanip>
    #include<stack>
    #include<string.h>
    
    const int maxnum=101;
    int mymap[maxnum][maxnum];
    int n;
    int fa[maxnum];
    struct edge{
        int point1;
        int point2;
        int weight;
        edge(int _point1,int _point2,int _weight)
        {
            point1=_point1;
            point2=_point2;
            weight=_weight;
        }
    };
    int cmp(edge a,edge b)
    {
        return a.weight<b.weight;
    }
    int findfa(int x)
    {
        return fa[x]==x?x:(fa[x]=findfa(fa[x]));
    }
    
    void mergefa(int x,int y)
    {
        fa[findfa(x)]=findfa(fa[y]);
    }
    
    void kruscal()
    {
        vector<edge> edges;
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                edges.push_back(edge(i,j,mymap[i][j]));
            }
        }
        sort(edges.begin(),edges.end(),cmp);
        
        int m=n*(n-1)/2;
        int cnt=0;
        int ans=0;
        for(int i=0;i<m;i++)
        {
            int x1=edges[i].point1;
            int x2=edges[i].point2;
            int fa1=findfa(x1);
            int fa2=findfa(x2);
            
            if(fa1!=fa2)
            {
                mergefa(x1,x2);
                cnt+=1;
                ans+=edges[i].weight;
                if(cnt>=n-1) break;
            }
        }
        
        cout<<ans<<endl;
        
    }
    int main()
    {
        
        while(cin>>n)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    cin>>mymap[i][j];
                }
            }
            for(int i=0;i<=n;i++)
                fa[i]=i;
            int m;
            cin>>m;
            for(int i=0;i<m;i++)
            {
                int x,y;
                cin>>x>>y;
                mymap[x-1][y-1]=mymap[y-1][x-1]=0;
            
            }
            kruscal();
            
        }
        return 0;
    }
    
    /*
     
     Sample Input
     3
     0 990 692
     990 0 179
     692 179 0
     1
     1 2
     
     
     Sample Output
     179
    
     
     */
    

      

  • 相关阅读:
    从汇编看c++中的placement operator new
    从汇编看c++的new和delete
    从汇编看c++中全局对象和全局变量
    javascript中的this
    好工具
    js压缩解压工具
    IE的documentMode属性
    77. sqlserver 锁表解决方式
    75. ID重新走过,备份表
    5. Java中序列化的serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/wuxiangli/p/6389323.html
Copyright © 2011-2022 走看看