专题四 第一道题

1.题目编号:1001

2.简单题意:有N个村庄,你需要建造道路使所有的村庄可以连在一起,如果有ABC三个村庄,AB之间有一条道路,AC之间也有一条道路,那么BC之间因为这两条线也能连在一起。我们已经知道有的村庄之间有已经建造的道路,求使所有村庄连在一起需要再建造的道路的最小值。

3.解题思路形成过程:看到这个题就想到是一个求最小生成树的,但是他给定了一些已经建造好的道路。每条边有三个属性,起点、终点和权值,如果起点和终点相反,只记录一个就可以,然后用一个数组将每个顶点初始化为1到n,如果已经建造好的道路则把起点和终点都置成起点值,然后调用自己写的Kruskal函数,记录m减去q个边的权值和。

4.感悟:在数据结构上学习到了图,其中求图的最小生成树学了两种方法,prim算法和kruskal算法。虽然老师在讲的时候很简单,在纸上找到一个图的最小生成树也很容易,但是真的等到写代码的时候还是无从下手。现在可以用上这方面的知识了,而且数据结构快要考试了,正好复习一下~吐舌头

5.AC的代码:

 #include<iostream>
 #include<algorithm>
  using namespace std;
  struct node{
      int from;
      int to;
      int w;
      };
node  edge[102*100];
  int parent[102];
  bool cmp(node a,node b)
 {
     if(a.w<=b.w) return true;
     return false;
 }
//查找已经建完道路的顶点
int find(int a)
{
    if(a!=parent[a])
        return find(parent[a]);
    else return a;
}
 int kruskal(int n,int m)
 {
     sort(edge,edge+m,cmp);//将边的权值从小到大排序
     int i,x,y,ans=0;
    for(i=0;i<m;i++)
     {
        x=edge[i].from;
        y=edge[i].to;
        x=find(x);
        y=find(y);
        if(x!=y)
        {
            ans+=edge[i].w;
            parent[y]=x;
        }
}
   return ans;
 }


 int main()
 {
     int n,q,k,i,j,m;
     while(cin>>n)
     {
         m=0;
        for(i=1;i<=n;i++)
         {
             for(j=1;j<=n;j++)
             {
                 cin>>k;
                if(i>=j) continue;//标记过的不用重复记录
                 edge[m].from=i;
                 edge[m].to=j;
                 edge[m].w=k;
            m++;
           }
        }
        for(k=1;k<=n;k++)
        parent[k]=k;
         cin>>q;
         //将建完的道路的起点和终点都置为相同的起点
        for(k=1;k<=q;k++)
        {
            cin>>i>>j;
           i=find(i);
            j=find(j);
             parent[j]=i;
      }
      //n个点,m条边
        cout<<kruskal(n,m)<<endl;
     }
     return 0;
 }

原题:

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. <br><br>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.<br>
 

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.<br><br>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.<br>
 

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. <br>
 

Sample Input
  
  
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output
  
  
179


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值