ccf 201812-4 数据中心

其实就是转换成求最小生成树中的最大支路,套模板

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define M 50009
using namespace std;
const int maxn=2e6+10,mod=100003;
#define pb push_back
int n,m,ans;
inline int read(){
	char ch;
	int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9')
		if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9')
	{
		res=(res<<1)+(res<<3)+(ch^48);//注意位运算的优先级别
		ch=getchar();
	}
	return f*res;
}

struct Edge{
    int s,e,w;
    Edge(int ss,int ee,int ww): s(ss),e(ee),w(ww){ }
    Edge() {}
    bool operator< (const Edge &e1) const{
        return w<e1.w;
    }
};
vector<Edge> edges;
vector <int>parent;
int GetRoot(int a)
{
    if(parent[a]==a)
        return a;
    parent[a]=GetRoot(parent[a]);
    return parent[a];
}
void Merge(int a,int b){
    int p1=GetRoot(a);
    int p2=GetRoot(b);
    if(p1==p2) return ;
    parent[p2]=p1;
}
int main(){
    int i,j,k,root;
    n=read();m=read();root=read();
    for(i=0;i<=n;i++) parent.push_back(i);
    for(k=0;k<m;k++)
    {
            int w;
        i=read();j=read();
        w=read();
        edges.push_back(Edge(i,j,w));
         edges.push_back(Edge(j,i,w));
    }
    sort(edges.begin(),edges.end());
    int done=0;int totallen=0;
    for(i=0;i<edges.size();i++)
    {
        if(GetRoot(edges[i].s)!=GetRoot(edges[i].e)){
            Merge(edges[i].e,edges[i].s);
            ++done;if(edges[i].w>totallen) totallen=edges[i].w;
        }
        if(done==n-1) break;
    }
    printf("%d\n",totallen);
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值