(UVA) Dark Roads - Solution 1
I used Kruskal's algorithm to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public static int[] nodeParent;
public static int[] depth;
public static int root(int n) {
int currN = n;
while (nodeParent[currN] != currN) {
currN = nodeParent[currN];
}
return currN;
}
public static boolean union(int n1, int n2) {
int rootN1 = root(n1);
int rootN2 = root(n2);
if (rootN1 != rootN2) {
if (depth[rootN1] >= depth[rootN2]) {
nodeParent[rootN2] = nodeParent[rootN1];
if (depth[rootN1] == depth[rootN2]) {
depth[rootN1]++;
}
}
else {
nodeParent[rootN1] = nodeParent[rootN2];
}
return true;
}
return false;
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numJunctions = sc.nextInt();
int numRoads = sc.nextInt();
while (numJunctions != 0 || numRoads != 0) {
ArrayList<Edge> edges = new ArrayList<Edge>();
for (int i = 0; i < numRoads; i++) {
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int cost = sc.nextInt();
edges.add(new Edge(n1, n2, cost));
}
Collections.sort(edges);
nodeParent = new int[numJunctions];
depth = new int[numJunctions];
for (int i = 0; i < numJunctions; i++) {
nodeParent[i] = i;
depth[i] = 0;
}
int save = 0;
for (int i = 0; i < edges.size(); i++) {
if (!union(edges.get(i).n1, edges.get(i).n2)) {
save += edges.get(i).cost;
}
}
System.out.println(save);
numJunctions = sc.nextInt();
numRoads = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge implements Comparable<Edge> {
int n1;
int n2;
int cost;
public Edge(int n1, int n2, int cost) {
this.n1 = n1;
this.n2 = n2;
this.cost = cost;
}
public int compareTo(Edge e) {
return this.cost-e.cost;
}
}
import java.io.*;
import java.util.*;
class Main {
public static int[] nodeParent;
public static int[] depth;
public static int root(int n) {
int currN = n;
while (nodeParent[currN] != currN) {
currN = nodeParent[currN];
}
return currN;
}
public static boolean union(int n1, int n2) {
int rootN1 = root(n1);
int rootN2 = root(n2);
if (rootN1 != rootN2) {
if (depth[rootN1] >= depth[rootN2]) {
nodeParent[rootN2] = nodeParent[rootN1];
if (depth[rootN1] == depth[rootN2]) {
depth[rootN1]++;
}
}
else {
nodeParent[rootN1] = nodeParent[rootN2];
}
return true;
}
return false;
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numJunctions = sc.nextInt();
int numRoads = sc.nextInt();
while (numJunctions != 0 || numRoads != 0) {
ArrayList<Edge> edges = new ArrayList<Edge>();
for (int i = 0; i < numRoads; i++) {
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int cost = sc.nextInt();
edges.add(new Edge(n1, n2, cost));
}
Collections.sort(edges);
nodeParent = new int[numJunctions];
depth = new int[numJunctions];
for (int i = 0; i < numJunctions; i++) {
nodeParent[i] = i;
depth[i] = 0;
}
int save = 0;
for (int i = 0; i < edges.size(); i++) {
if (!union(edges.get(i).n1, edges.get(i).n2)) {
save += edges.get(i).cost;
}
}
System.out.println(save);
numJunctions = sc.nextInt();
numRoads = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge implements Comparable<Edge> {
int n1;
int n2;
int cost;
public Edge(int n1, int n2, int cost) {
this.n1 = n1;
this.n2 = n2;
this.cost = cost;
}
public int compareTo(Edge e) {
return this.cost-e.cost;
}
}
Comments
Post a Comment