Constructing Roads POJ - 2421

12 篇文章 0 订阅
10 篇文章 0 订阅

//刚开始看错题了..看例子以为给你n行,每行意思为a, b之间的距离为dist.(有向).一直找不到自己错在哪 ,,,  然后....看了看别人的发现是无向....n*n位邻接矩阵.............


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;

const int N = 1000+5;

struct Edge{
    int u, v, dist;

    Edge(){
        u = 0;
        v = 0;
        dist = 0;
    };
    Edge(int a, int b, int c){
        u = a;
        v = b;
        dist = c;
    }

}edge;

bool operator <(Edge a, Edge b)
{
    return a.dist > b.dist;
}

priority_queue<Edge> q;
int judge[N][N] = {0};

int pre[N*N/2] = {0};

void init()
{
//    memset(judge, 0, sizeof(judge));
    for(int i=0; i<N*N/2; i++){
        pre[i] = i;
    }
}



int Find(int a)
{
    return pre[a] == a ? a : Find(pre[a]);
}

void Union(int a, int b)
{
    a = Find(a), b = Find(b);

    if(a == b){
        return;
    }
    else{
        pre[a] = b;
        return;
    }
}

void Kruskal()
{
    int sum = 0;

    while(!q.empty()){
        edge = q.top();
        q.pop();

        if(Find(edge.u) != Find(edge.v)){
            sum += edge.dist;
            Union(edge.u, edge.v);
        }
    }
    cout << sum << endl;
}

int main() {
    int n;


    while(cin >> n) {
        while (!q.empty()) {
            q.pop();
        }


        init();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int u, v, l;
                // cin >> u >> v >> l;
                cin >> l;

                //        judge[u][v] = 1;
                //           judge[v][u] = 1;

                q.push(Edge(i, j, l));
            }
        }

            int k, a, b;
            cin >> k;

            while (k--) {
                cin >> a >> b;
                Union(a, b);
                //      judge[a][b] = 0;
//        judge[b][a] = 0;
            }
            Kruskal();
        }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值