(UVA) Dark Roads - Solution 2
If you want to see another solution for this problem, click here.
I used Prim's algorithm to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Edge>> adjList;
public static Comparator<Edge> costComparator = new Comparator<Edge>() {
public int compare(Edge e1, Edge e2) {
return e1.cost-e2.cost;
}
};
public static int prim(int start, int numJunctions) {
Queue<Edge> queue = new PriorityQueue<Edge>(numJunctions, costComparator);
Edge e = new Edge(start, 0);
queue.add(e);
HashSet<Integer> visited = new HashSet<Integer>();
int cost = 0;
while (queue.size() > 0) {
Edge curr = queue.poll();
int currJunction = curr.junction;
int currCost = curr.cost;
if (visited.contains(currJunction)) {
continue;
}
visited.add(currJunction);
cost += currCost;
if (visited.size() == numJunctions) {
return cost;
}
ArrayList<Edge> reachJunctions = adjList.get(currJunction);
for (int i = 0; i < reachJunctions.size(); i++) {
if (!visited.contains(reachJunctions.get(i).junction)) {
queue.add(new Edge(reachJunctions.get(i).junction, reachJunctions.get(i).cost));
}
}
}
return cost;
}
public static int reader (BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numJunctions = reader(br);
int numRoads = reader(br);
while (numJunctions != 0 || numRoads != 0) {
adjList = new HashMap<Integer, ArrayList<Edge>>();
for (int i = 0; i < numJunctions; i++) {
adjList.put(i, new ArrayList<Edge>());
}
int totalCost = 0;
for (int i = 0; i < numRoads; i++) {
int x = reader(br);
int y = reader(br);
int z = reader(br);
adjList.get(x).add(new Edge(y, z));
adjList.get(y).add(new Edge(x, z));
totalCost += z;
}
int finalCost = prim(0, numJunctions);
System.out.println(totalCost-finalCost);
numJunctions = reader(br);
numRoads = reader(br);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
int junction;
int cost;
public Edge(int j, int c) {
junction = j;
cost = c;
}
}
I used Prim's algorithm to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Edge>> adjList;
public static Comparator<Edge> costComparator = new Comparator<Edge>() {
public int compare(Edge e1, Edge e2) {
return e1.cost-e2.cost;
}
};
public static int prim(int start, int numJunctions) {
Queue<Edge> queue = new PriorityQueue<Edge>(numJunctions, costComparator);
Edge e = new Edge(start, 0);
queue.add(e);
HashSet<Integer> visited = new HashSet<Integer>();
int cost = 0;
while (queue.size() > 0) {
Edge curr = queue.poll();
int currJunction = curr.junction;
int currCost = curr.cost;
if (visited.contains(currJunction)) {
continue;
}
visited.add(currJunction);
cost += currCost;
if (visited.size() == numJunctions) {
return cost;
}
ArrayList<Edge> reachJunctions = adjList.get(currJunction);
for (int i = 0; i < reachJunctions.size(); i++) {
if (!visited.contains(reachJunctions.get(i).junction)) {
queue.add(new Edge(reachJunctions.get(i).junction, reachJunctions.get(i).cost));
}
}
}
return cost;
}
public static int reader (BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numJunctions = reader(br);
int numRoads = reader(br);
while (numJunctions != 0 || numRoads != 0) {
adjList = new HashMap<Integer, ArrayList<Edge>>();
for (int i = 0; i < numJunctions; i++) {
adjList.put(i, new ArrayList<Edge>());
}
int totalCost = 0;
for (int i = 0; i < numRoads; i++) {
int x = reader(br);
int y = reader(br);
int z = reader(br);
adjList.get(x).add(new Edge(y, z));
adjList.get(y).add(new Edge(x, z));
totalCost += z;
}
int finalCost = prim(0, numJunctions);
System.out.println(totalCost-finalCost);
numJunctions = reader(br);
numRoads = reader(br);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
int junction;
int cost;
public Edge(int j, int c) {
junction = j;
cost = c;
}
}
Comments
Post a Comment