zoukankan      html  css  js  c++  java
  • HDOJ 1102 Constructing Roads (最小生成树)

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 17641    Accepted Submission(s): 6711


    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
     

    Source
     
    题意:先给出一个n,然后是n*n的矩阵,第i行第j列的值k表示:村庄i,j之间有一条长度k的路径。然后给出一个q,接着q行,给出两个数a,b表示a,b之间的路已经建好。问,把所有村庄连接起来需要至少得路径长度。
    最小生成树模板题,用kruskal算法,对于已经修好路的村庄,只要把它们对应连通分量设置成相同的值就行了。。这题交了很多遍一直wa后来发现是多组输入。wtf题目并没有说啊,以后还是多小心一点,毕竟多组输入的代码给一组的是不会错的。
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #define MAX 5005
    int pa[MAX];
    int m,n,t,r;
    using namespace std;
    struct edge
    {
        int beg,en,r;
    };
    edge edges[MAX];
    bool cmp(edge x,edge y)
    {
        return x.r<y.r;
    }
    int dfs(int x)
    {
        if(pa[x]!=x)pa[x]=dfs(pa[x]);
        return pa[x];
    }
    int kruskal()
    {
        int ans=0;
        sort(edges,edges+m,cmp);
        for(int i=0;i<m;i++)
        {
            int beg=edges[i].beg,en=edges[i].en;
            if(dfs(beg)!=dfs(en))
            {
                pa[dfs(beg)]=dfs(en);
                ans+=edges[i].r;
            }
        }
       return ans;
    }
    //n:节点数目 m:边数目
    int main()
    {
       while(~scanf("%d",&n))
       {
    
        m=0;
       for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
       {
          scanf("%d",&r);
          if(j>i)
          {
            edges[m].beg=i;
            edges[m].en=j;
            edges[m++].r=r;
          }
       }
       for(int i=0;i<=n;i++)
            pa[i]=i;
       scanf("%d",&t);
       while(t--)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           if(a>b)swap(a,b);
             if(dfs(a)!=dfs(b))
                pa[dfs(a)]=dfs(b);
       }
       printf("%d
    ",kruskal());
    
    
       }
    
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Excel文档间的数据替换 ---电脑版APP 自动操作魔法师
    【css】zSass
    【javascript】利用 a 标签自动解析 url
    【javascript】console 让 js 调试更简单
    【规范】javascript 变量命名规则
    【规范】前端编码规范——一般规范
    【jquery】一款不错的音频播放器——Amazing Audio Player
    【分享】分享一款不错的网页视频播放器
    【分享】分享一个值得前端开发收藏的网站
    【html】button按钮的一些问题
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768441.html
Copyright © 2011-2022 走看看