这个做法csp官网只能95
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main
{
private static int N=100010,M=200010;
private static int[] p=new int[N];
private static int n,m;
static Comparator<Node> cmp=new Comparator<Node>()
{
//按照时间花费从小到大排序
@Override
public int compare(Node o1, Node o2)
{
return o1.c-o2.c;
}
};
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
n=scan.nextInt();
m=scan.nextInt();
for(int i=1;i<=n;i++)
p[i]=i;
Node[] nodes=new Node[m];
for(int i=0;i<m;i++)
{
int a=scan.nextInt();
int b=scan.nextInt();
int c=scan.nextInt();
nodes[i]=new Node(a,b,c);
}
Arrays.sort(nodes,cmp);
int res=0;
for(int i=0;i<m;i++)
{
int a=nodes[i].a,b=nodes[i].b,c=nodes[i].c;
int pa=find(a),pb=find(b);
if(pa!=pb)
{
p[pa]=pb;
res=Math.max(res, c);//记录下时间最大值
if(find(1)==find(n))//如果1号和n号联通后就直接退出了
break;
}
}
System.out.println(res);
scan.close();
}
private static int find(int a)
{
if(p[a]!=a)
p[a]=find(p[a]);
return p[a];
}
private static class Node
{
private int a;
private int b;
private int c;
public Node(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
}
}
我也写了一遍,csp是100