CCFCSP 201812-4 数据中心

思路

  • 虽然题目看起来稍微有点复杂(比大模拟题好多了www),但其实就是非常简单的最小生成树,因为kruskal记的比较清楚且空间复杂度稍微低一点就用这个了。
  • 注意在找到父亲结点的时候,也需要修改查找父节点一路上的点的father值,之后查找就会减少递归次数,不考虑这点的话只能拿到70分(超时)

代码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
int father[50005];

struct edge{
    int first;
    int second;
    LL len;
    
};

bool cmp(edge e1,edge e2){
    return e1.len<e2.len;
}

int findfa(int x){
    if(father[x]!=x)
        father[x]=findfa(father[x]);//不加这句话只能拿到70分超时
    return father[x];
}

void setunion(int x,int y){
    int yfa=findfa(y);
    father[yfa]=findfa(x);
}

int main(){
    ios::sync_with_stdio(false);
    int n=0,m=0,root=0;
    cin>>n>>m>>root;
    vector<edge> save;
    int init=0;
    while(init<n){
        father[init]=init;
        init++;
    }
    while(m--){
		int v=0,u=0;
        LL t=0;
        cin>>v>>u>>t;
        if(v>u){
            int temp=u;
            u=v;
            v=temp;
        }
        edge nowe;
        nowe.first=v;
        nowe.second=u;
        nowe.len=t;
        save.push_back(nowe);
    }

    sort(save.begin(),save.end(),cmp);
    
    int count=0;
    LL max=0;
    for(int i=0;i<save.size();i++){
        if(findfa(save[i].first)!=findfa(save[i].second)){
            setunion(save[i].first,save[i].second);
            max=save[i].len;
            count++;
            if(count==n-1)
                break;
        }
        
    }

    cout<<max<<endl;
    return 0;
}

/*
4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值