CCF CSP 最优灌溉 JAVA 201412_4 100分

该博客介绍了使用JAVA语言解决CCF CSP中的最优灌溉问题,通过应用Kruskal算法来求解最小生成树。
摘要由CSDN通过智能技术生成

用的并查集和优先队列去解的。

其实就是Kruskal算法。

最小生成树问题。

package csp2014_12_4;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

class Edge implements Comparable<Edge>{
	int s,e,cost;
	Edge(int a,int b,int c){
		s=a;e=b;cost=c;
	}
	public Edge() {}
	@Override
	public int compareTo(Edge o) {
		if(this.cost-o.cost==0) 	return this.s-o.s;
		return this.cost-o.cost;
	}
}

public class Main{
	static Queue<Edge> q = new PriorityQueue<Edge>();

	static int pre[];
	static int find(int x){
		if(pre[x]==x) return x;
		else return pre[x] = find(pre[x]);
	}
	static void link(int x,int y){
		int xx = find(x);
		int yy = find(y);
		if(xx>yy) pre[xx] = yy;
		else pre[yy] = pre[xx];
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = sc.nextInt();
			q.add(new Edge(a,b,c));
		}
		
		pre = new int[n+1];
		for (int i = 0; i < pre.length; i++) {
			pre[i] = i;
		}
		
		int ans = 0;
		Edge temp = new Edge();
		whil
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值