import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class kug { public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { int cups = in.nextInt(); final PriorityQueue<Edge> queue = new PriorityQueue<Edge>((cups + 1) * cups / 2, new Comparator<Edge>() { @Override public int compare(Edge e1, Edge e2) { return e1.cost - e2.cost; } }); for (int i = 0; i < cups; i++) { for (int k = 0; k < cups - i; k++) { queue.add(new Edge(i, i + k + 1, in.nextInt())); } } final LinkedList<List<Integer>> trees = new LinkedList<List<Integer>>(); for (int i = 0; i <= cups; i++) { List<Integer> tree = new ArrayList<Integer>(); tree.add(i); trees.add(tree); } long sum = 0; while (trees.size() > 1) { Edge edge = queue.remove(); Iterator<List<Integer>> it = trees.iterator(); boolean nextEdge = false; while (!nextEdge && it.hasNext()) { List<Integer> tree = it.next(); if (tree.contains(edge.node1) || tree.contains(edge.node2)) { int search = tree.contains(edge.node1) ? edge.node2 : edge.node1; if (tree.contains(search)) { nextEdge = true; break; } while (it.hasNext()) { List<Integer> tree2 = it.next(); if (tree2.contains(search)) { sum += edge.cost; tree.addAll(tree2); it.remove(); nextEdge = true; break; } } } } } System.out.println(sum); } } private static class Edge { public final int node1; public final int node2; public final int cost; public Edge(int node1, int node2, int cost) { this.node1 = node1; this.node2 = node2; this.cost = cost; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class kug { public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { int cups = in.nextInt(); final PriorityQueue<Edge> queue = new PriorityQueue<Edge>((cups + 1) * cups / 2, new Comparator<Edge>() { @Override public int compare(Edge e1, Edge e2) { return e1.cost - e2.cost; } }); for (int i = 0; i < cups; i++) { for (int k = 0; k < cups - i; k++) { queue.add(new Edge(i, i + k + 1, in.nextInt())); } } final LinkedList<List<Integer>> trees = new LinkedList<List<Integer>>(); for (int i = 0; i <= cups; i++) { List<Integer> tree = new ArrayList<Integer>(); tree.add(i); trees.add(tree); } long sum = 0; while (trees.size() > 1) { Edge edge = queue.remove(); Iterator<List<Integer>> it = trees.iterator(); boolean nextEdge = false; while (!nextEdge && it.hasNext()) { List<Integer> tree = it.next(); if (tree.contains(edge.node1) || tree.contains(edge.node2)) { int search = tree.contains(edge.node1) ? edge.node2 : edge.node1; if (tree.contains(search)) { nextEdge = true; break; } while (it.hasNext()) { List<Integer> tree2 = it.next(); if (tree2.contains(search)) { sum += edge.cost; tree.addAll(tree2); it.remove(); nextEdge = true; break; } } } } } System.out.println(sum); } } private static class Edge { public final int node1; public final int node2; public final int cost; public Edge(int node1, int node2, int cost) { this.node1 = node1; this.node2 = node2; this.cost = cost; } } } |