2016SDAU课程练习四1001 Problem A

Problem A

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 179 Accepted Submission(s) : 68
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

题目大意:

            有几个村庄,从1到,你应该修建一些道路,这样每一个村庄都可以互相连接。我们说,2个村一个和二个是相连的,如果有一条路在一个或二个之间,或存在一个村,这样,有一个和丙之间的道路,和。
我们知道,一些村庄和你的工作之间已经有一些道路,修建一些道路,这样所有的村庄都连接起来,所有道路的长度都是最小的。接下来的N行,其中包含N个整数i,而这些N整数j的距离(距离应该是一个整数,在[ 1,1000 ])村庄i,J 然后之间有一个整数q(0 <= q≤n(n 1)/ 2)。然后来问线,每行包含2个整数1和2(1 < = 1),这意味着村之间的道路已建成,这是所有的道路的长度,以建立这样的所有村庄连接,这个值是最低。
思路:有的路已经修好的处理方式:将该条边的权值置为0.map[a][b] = map[b][a] = 0;//对于已经存在的边,我们把他的权值置为0即可。
连接信息由矩阵转换成边的形式。
int cnt = 1;
for(i = 1 ; i <= n ; ++i){//将链接信息有矩阵的形式转换成边的形式
for(j = 1 ; j <= i ; ++j){
edges[cnt].begin = i;
edges[cnt].end = j;
edges[cnt++].weight = map[i][j];
}
}
ac代码
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int visit[1002];
int maa[105][105];
int main()
{
    int t;
    int x,y;
    int f;
    int n;
    int i,j;
    while(cin>>t)
    {
        memset(visit,0,sizeof(visit));
        for(i=1;i<=t;i++)
        {
             for(j=1;j<=t;j++)
              {
                  scanf("%d",&f);
                  maa[i][j]=f;
              }
        }
        cin>>n;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            scanf("%d",&y);
            maa[x][y]=0;
            maa[y][x]=0;
        }
        int min1;
        int k;
        int sum=0;
        for(i=2;i<=t;i++)
        {
            min1=100000;
            for(j=2;j<=t;j++)
            {
                if(visit[j]==0&&min1>maa[1][j])
                {
                    min1=maa[1][j];
                    k=j;
                }
            }
            sum+=min1;
            visit[k]=1;
            for(j=2;j<=t;j++)
            {
                if(visit[j]==0&&maa[k][j]<maa[1][j])
                    maa[1][j]=maa[k][j];
            }




            }
            cout<<sum<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值