一、题目
问题描述
试题编号: | 201812-4 |
试题名称: | 数据中心 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: | ![](http://118.190.20.162/RequireFile.do?fid=DagRh9nq)
![](http://118.190.20.162/RequireFile.do?fid=A6Fg87D2) 样例输入 4 5 1 1 2 3 1 3 4 1 4 5 2 3 8 3 4 2 样例输出 4 样例说明 下图是样例说明。
![](http://118.190.20.162/RequireFile.do?fid=2LYA5Jrq)
![](http://118.190.20.162/RequireFile.do?fid=RD5AggLj) |
二、思路
待更新;并查集
三、代码
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)