Skip to content
Snippets Groups Projects
Commit d0cf55f5 authored by rkuchar's avatar rkuchar
Browse files

Zkouska 1

parent bce6e166
No related branches found
No related tags found
No related merge requests found
package pal;
/**
* @Author Roman Kuchár <kucharrom@gmail.com>.
*/
class Connection {
private int from;
private int to;
private int length;
public Connection(int from, int to, int length) {
this.from = from;
this.to = to;
this.length = length;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getTo() {
return to;
}
public void setTo(int to) {
this.to = to;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
}
\ No newline at end of file
package pal;
import java.util.Comparator;
/**
* @Author Roman Kuchár <kucharrom@gmail.com>.
*/
public class ConnectionLengthComparator implements Comparator<Connection> {
private int magicLength;
public ConnectionLengthComparator(int magicLength) {
this.magicLength = magicLength;
}
@Override
public int compare(Connection o1, Connection o2) {
int o1Length = o1.getLength() == magicLength ? 0 : o1.getLength();
int o2Length = o2.getLength() == magicLength ? 0 : o2.getLength();
if (o1Length > o2Length) {
return 1;
} else if (o1Length < o2Length) {
return -1;
} else {
return 0;
}
}
}
package pal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* @Author Roman Kuchár <kucharrom@gmail.com>.
*/
public class Main {
private static List<Connection> connections;
private static Map<Integer, Integer> sameLengthCount = new HashMap<>();
private static int detectorsCount;
private static int maxLengthCount = 0;
// min length of result spanning tree
private static Long minResultLength = Long.MAX_VALUE;
public static void main(String[] args) throws IOException {
readData();
Map<Integer, Integer> sameLengthCountSorted = new LinkedHashMap<>();
//sort by value, and reserve, 10,9,8,7,6...
sameLengthCount.entrySet().stream()
.sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed())
.forEachOrdered(x -> sameLengthCountSorted.put(x.getKey(), x.getValue()));
for (Integer length : sameLengthCountSorted.keySet()) {
if (sameLengthCountSorted.get(length) != maxLengthCount) {
break;
}
kruskalAlgorithm(connections, detectorsCount, length);
}
System.out.println(minResultLength);
}
public static void kruskalAlgorithm(List<Connection> edges, int nodeCount, int magicLength) {
UnionFind unionFind = new UnionFind(nodeCount);
long minLength = 0;
// int minMagicLength = 0;
int spanningTreeSize = 0;
edges.sort(new ConnectionLengthComparator(magicLength));
int i = 0;
while (i != edges.size() && spanningTreeSize != nodeCount - 1) {
Connection connection = edges.get(i);
if (unionFind.find(connection.getFrom()) != unionFind.find(connection.getTo())) {
spanningTreeSize++;
unionFind.union(connection.getFrom(), connection.getTo());
minLength += connection.getLength();
// minMagicLength += connection.getLength() == magicLength ? 0 : connection.getLength();
}
i++;
}
// System.out.println(magicLength + ": " + minMagicLength + " " + minLength);
if (minResultLength > minLength) {
minResultLength = minLength;
}
}
private static void readData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
detectorsCount = readInt(br);
int connectionsCount = readInt(br);
connections = new ArrayList<>(connectionsCount);
for (int i = 0; i < connectionsCount; i++) {
// data have node number from one. Need to decrement so it will be from 0.
int from = readInt(br) - 1;
int to = readInt(br) - 1;
int length = readInt(br);
connections.add(new Connection(from, to, length));
Integer integer = sameLengthCount.get(length);
if (integer == null) {
sameLengthCount.put(length, 1);
} else {
int newLengthCount = integer + 1;
sameLengthCount.put(length, newLengthCount);
if (maxLengthCount < newLengthCount) {
maxLengthCount = newLengthCount;
}
}
}
}
private static int readInt(BufferedReader in) throws IOException {
int ret = 0;
boolean dig = false;
for (int c = 0; (c = in.read()) != -1; ) {
if (c >= '0' && c <= '9') {
dig = true;
ret = ret * 10 + c - '0';
} else if (dig) break;
}
return ret;
}
}
package pal;
/**
* @Author Roman Kuchár <kucharrom@gmail.com>.
*/
class UnionFind {
private int[] parent;
private int[] rank;
public UnionFind(int max) {
parent = new int[max];
rank = new int[max];
for (int i = 0; i < max; i++) {
parent[i] = i;
}
}
public int find(int i) {
int p = parent[i];
if (i == p) {
return i;
}
return parent[i] = find(p);
}
public void union(int i, int j) {
int root1 = find(i);
int root2 = find(j);
if (root2 == root1) return;
if (rank[root1] > rank[root2]) {
parent[root2] = root1;
} else if (rank[root2] > rank[root1]) {
parent[root1] = root2;
} else {
parent[root2] = root1;
rank[root1]++;
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment