CCF 201812-4 数据中心 Java

一、题目

问题描述

试题编号:201812-4
试题名称:数据中心
时间限制:1.0s
内存限制:512.0MB
问题描述:


样例输入

4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2

样例输出

4

样例说明

  下图是样例说明。

二、思路

待更新;并查集

三、代码

 

import java.util.*;
public class Main{
	static int[] parrent;
	static int max=0;
	static class edge implements Comparable<edge>{
		int p1,p2;
		int cost;
		edge(int p1,int p2,int cost){
			this.p1=p1;
			this.p2=p2;
			this.cost=cost;
		}
		public int compareTo(edge e){
			return this.cost-e.cost;
		}
	}
	static int find(int x){
		while(x!=parrent[x]){
			parrent[x]=parrent[parrent[x]];
			x=parrent[x];
		}
		return x;
	}
	static boolean unite(int x,int y){
		x=find(x);
		y=find(y);
		if(x!=y) {
			parrent[x]=y;
			return true;
		}
		return false;
	}
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		int node=in.nextInt();
		int num=in.nextInt();
		int root=in.nextInt();
		edge[] arr=new edge[num];
		parrent=new int[num];
		for(int i=0;i<num;i++)
			parrent[i]=i;
		for(int i=0;i<num;i++){
			int e1=in.nextInt();
			int e2=in.nextInt();
			int c=in.nextInt();
			arr[i]=new edge(e1,e2,c);
		}
		Arrays.sort(arr);
		for(int i=0;i<num;i++){
			edge e=arr[i];
			if(unite(e.p1,e.p2)){
				max=e.cost;
				node--;
			}
			if(node==1) break;
			
		}
		System.out.println(max);
	}
}

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值