hrbust 1101 Bombs of HRBUST(最小生成树)

Bombs of HRBUST
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 200(100 users)Total Accepted: 116(93 users)Rating: Special Judge: No
Description

Terrorists have placed bombs in each building of HRBUST during Summer Holiday.
They plan to assign fuse to make all the bombs connected so that they can destroy HRBUST just by detonating only one bomb, and the terrorists need 1 minute of time to assign 1 meter of fuse.
If bomb x and bomb y are connected by fuse, and one of them exploded, the other will explode immediately.(So they don't need to connect each pair of bombs.)
Now, you have got the distance between each pair of bombs, and you need to calculate the minimal time in minute they need to make all the bombs connected.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.
For each test case:
Line 1. There are one number n(2≤n≤100), meaning the number of bombs.
Line 2..N + 1: There are one n * n matrix MAT (1≤MATij≤1000), the element MATij means the distance between bomb i and bomb j.
(It is guaranteed that the element on the diagonal line will always be 0 and MATij = MATji)

Output

For each test case:
Line 1. Output the minimal time in minute the terrorists need to achieve their goal.

Sample Input

2
2
0 1
1 0
3
0 2 1
2 0 3
1 3 0

Sample Output

1
3

Hint

For Sample 1: They need to connect bomb 1 and bomb 2, the distance between them are 1m, so the terrorists need 1 minute.
For Sample 2: They just need to connect bomb 1 and bomb 2, bomb 1 and bomb 3, and the total length of fuse is 3m, so the terrorists need 3 minutes.

Author
最小生成树裸题。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[103][103];
int pre[1003];
struct data
{
    int x,y;
    int c;
}num[19993];
int find(int x)
{
    if(x!=pre[x])
    {
        return pre[x]=find(pre[x]);
    }
    return x;
}
int cmp(data a,data b)
{
    return a.c<b.c;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            pre[i]=i;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        int m=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<i; j++)
            {
                num[m].x=i;
                num[m].y=j;
                num[m].c=a[i][j];
                m++;
            }
        }
        sort(num,num+m,cmp);
        int cou=1;
        int res=0;
        for(int i=0;i<m;i++)
        {
            if(find(num[i].x)!=find(num[i].y))
            {
                cou++;
               // printf("%d\n",res);
                res+=a[num[i].x][num[i].y];
                pre[find(num[i].x)]=find(num[i].y);
            }
            if(cou==n)
            {
                break;
            }
        }
        printf("%d\n",res);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值