Hust oj 1101 Bombs of HRBUST(Prim)

Bombs of HRBUST
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 184(91 users)Total Accepted: 107(84 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

裸Prim

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;

const int maxn = 305;
const int Inf = 0x3f3f3f;
int dis[maxn];
int map[maxn][maxn];
int vis[maxn];
int N;

void init()
{
    for(int i=0;i<N;i++)
    {
        dis[i] = map[i][0];
        vis[i] = 0;
    }
} //距离和访问数组初始化

int prim()
{
    init();
    dis[0] = 0; //起点距离初始化为0
    vis[0] = 1; //标记起点
    int ans = 0; //权值和初始化为0

    for(int i=0;i<N-1;i++)
    {
        int temp = Inf; // 记录最小边
        int flag; //纪录最小点
        for(int j=0;j<N;j++)
        {
            if(!vis[j] && dis[j] < temp)
            {
                temp = dis[j];
                flag = j;
            }
        } //找最小边
        vis[flag] = 1; //标记选出的边
        ans += dis[flag]; //把边加入生成树中
        for(int j=0;j<N;j++)
        {
            if(!vis[j] && dis[j] > map[flag][j])
                dis[j] = map[flag][j]; //更新最小权值
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
                scanf("%d",&map[i][j]); //邻接矩阵存图
        }
        int ans = prim();
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值