數據中心(CCF 201812-4)

第一波嘗試
思路:Kruskal算法求最小生成樹

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Node{
	int u;
	int v;
	int dis;
	public Node(int u, int v, int dis) {
		super();
		this.u = u;
		this.v = v;
		this.dis = dis;
	}
	@Override
	public String toString() {
		return "Node [u=" + u + ", v=" + v + ", dis=" + dis + "]";
	}
}

public class Main {

	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		
		int n = s.nextInt();      // 節點數
		int m = s.nextInt();      // 邊數
		int root = s.nextInt()-1; // 根節點
		
		ArrayList<Node> nodes = new ArrayList<Node>();
		
		// 輸入
		int u,v,t;
		for(int i=0;i<m;i++) {
			u = s.nextInt()-1;
			v = s.nextInt()-1;
			t = s.nextInt();
			
			nodes.add(new Node(u,v,t));
		}
				
		// 排序
		Collections.sort(nodes, new Comparator<Node>() {
			public int compare(Node o1, Node o2) {
				return o1.dis-o2.dis;
			}
		});
		
		// 構建最小生成樹
		// 初始化並查集
		int[] ufset = new int[n];
		for(int i=0;i<n;i++) {
			ufset[i] = i;
		}
		
		// 從小到大處理邊
		int cnt = 0;        // 總共應合併n-1次
		for(Node node:nodes) {
			int uroot = find(ufset,node.u);
			int vroot = find(ufset,node.v);
			if(uroot != vroot) {
				ufset[vroot] = uroot;
				cnt++;
				
				if(cnt==n-1) {
					System.out.println(node.dis);
					break;
				}
			}
			
			
		}
		
	}
	
	// 找根節點,並壓縮路徑
	public static int find(int[] ufset,int index) {
//		System.out.println("find"+index);
		int root = ufset[index];
		while(ufset[root]!=root) {
			root = ufset[root];
		}
		while(index!=root) {
			int next = ufset[index];
			ufset[index] = root;
			index = next;
		}
//		System.out.println("root"+root);
		return root;
	}
	
}

結果:90分,超時


第二波嘗試
修改了存結點的方式

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		
		int n = s.nextInt();      // 節點數
		int m = s.nextInt();      // 邊數
		int root = s.nextInt()-1; // 根節點
		
		// 輸入		
		ArrayList<int[]> nodeArr = new ArrayList<int[]>();
		for(int i=0;i<m;i++) {
			int[] nums = new int[3];
			nums[0] = s.nextInt()-1;
			nums[1] = s.nextInt()-1;
			nums[2] = s.nextInt();
			nodeArr.add(nums);
		}
		
        // 排序
		Collections.sort(nodeArr, new Comparator<int[]>(){
			public int compare(int[] o1,int[] o2) {
				return o1[2]-o2[2];
			}
		}
		);
		
		// 構建最小生成樹
		// 初始化並查集
		int[] ufset = new int[n];
		for(int i=0;i<n;i++) {
			ufset[i] = i;
		}
		
		// 從小到大處理邊
		int cnt = 0;        // 總共應合併n-1次
		for(int[] node:nodeArr) {
			int uroot = find(ufset,node[0]);
			int vroot = find(ufset,node[1]);
			if(uroot != vroot) {
				ufset[vroot] = uroot;
				cnt++;
				
				if(cnt==n-1) {
					System.out.println(node[2]);
					break;
				}
			}
		}
		
	}
	
	// 找根節點,並壓縮路徑
	public static int find(int[] ufset,int index) {
		int root = ufset[index];
		while(ufset[root]!=root) {
			root = ufset[root];
		}
		while(index!=root) {
			int next = ufset[index];
			ufset[index] = root;
			index = next;
		}
		return root;
	}
	
}

結果:得分100,時間使用 1.0s,空間使用 107.7MB
踩點過嘿嘿


測試1

輸入

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

輸出

4

測試2

輸入

6
10
1
1 2 10
1 3 16
1 4 14
2 4 15
3 4 14
2 3 24
4 5 23
4 6 8
3 6 16
5 6 22

輸出

22

測試3

輸入

6
9
1
1 2 1
3 5 2
4 6 3
2 5 4
1 3 5
1 4 6
3 4 7
2 3 8
5 6 9

輸出

6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章