(UVA) IP-TV - Solution 1
I used Prim's algorithm to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<String, 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(String start, int numCities) {
Queue<Edge> queue = new PriorityQueue<Edge>(numCities, costComparator);
queue.add(new Edge(start, 0));
HashSet<String> visited = new HashSet<String>();
int totalCost = 0;
while (queue.size() > 0) {
Edge curr = queue.poll();
String currCity = curr.city;
int currCost = curr.cost;
if (visited.contains(currCity)) {
continue;
}
visited.add(currCity);
totalCost += currCost;
if (visited.size() == numCities) {
return totalCost;
}
ArrayList<Edge> reachCities = adjList.get(currCity);
for (int i = 0; i < reachCities.size(); i++) {
queue.add(reachCities.get(i));
}
}
return -1;
}
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 numTests = reader(br);
for (int i = 0; i < numTests; i++) {
if (i > 0) {
System.out.println();
}
int numCities = reader(br);
int numLinks = reader(br);
String cityStart = "";
adjList = new HashMap<String, ArrayList<Edge>>();
for (int j = 0; j < numLinks; j++) {
String line = br.readLine();
String[] s = line.split("\\s");
String city1 = s[0];
String city2 = s[1];
int cost = Integer.parseInt(s[2]);
if (!adjList.containsKey(city1)) {
adjList.put(city1, new ArrayList<Edge>());
}
if (!adjList.containsKey(city2)) {
adjList.put(city2, new ArrayList<Edge>());
}
adjList.get(city1).add(new Edge(city2, cost));
adjList.get(city2).add(new Edge(city1, cost));
cityStart = city1;
}
System.out.println(prim(cityStart, numCities));
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
String city;
int cost;
public Edge(String city, int cost) {
this.city = city;
this.cost = cost;
}
}
import java.io.*;
import java.util.*;
class Main {
public static HashMap<String, 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(String start, int numCities) {
Queue<Edge> queue = new PriorityQueue<Edge>(numCities, costComparator);
queue.add(new Edge(start, 0));
HashSet<String> visited = new HashSet<String>();
int totalCost = 0;
while (queue.size() > 0) {
Edge curr = queue.poll();
String currCity = curr.city;
int currCost = curr.cost;
if (visited.contains(currCity)) {
continue;
}
visited.add(currCity);
totalCost += currCost;
if (visited.size() == numCities) {
return totalCost;
}
ArrayList<Edge> reachCities = adjList.get(currCity);
for (int i = 0; i < reachCities.size(); i++) {
queue.add(reachCities.get(i));
}
}
return -1;
}
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 numTests = reader(br);
for (int i = 0; i < numTests; i++) {
if (i > 0) {
System.out.println();
}
int numCities = reader(br);
int numLinks = reader(br);
String cityStart = "";
adjList = new HashMap<String, ArrayList<Edge>>();
for (int j = 0; j < numLinks; j++) {
String line = br.readLine();
String[] s = line.split("\\s");
String city1 = s[0];
String city2 = s[1];
int cost = Integer.parseInt(s[2]);
if (!adjList.containsKey(city1)) {
adjList.put(city1, new ArrayList<Edge>());
}
if (!adjList.containsKey(city2)) {
adjList.put(city2, new ArrayList<Edge>());
}
adjList.get(city1).add(new Edge(city2, cost));
adjList.get(city2).add(new Edge(city1, cost));
cityStart = city1;
}
System.out.println(prim(cityStart, numCities));
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
String city;
int cost;
public Edge(String city, int cost) {
this.city = city;
this.cost = cost;
}
}
Comments
Post a Comment