(UVA) Mice and Maze - Solution 1
In order to solve this problem, I used Dijkstra's algorithm.
For this solution, I reversed the edges because I try to reach the maximum amount of cells from my exit cell during the count-down timer.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Edge>> adjList;
public static Comparator<Edge> timeComparator = new Comparator<Edge>() {
public int compare(Edge e1, Edge e2) {
return e1.cost - e2.cost;
}
};
public static int dijkstra(int start, int numCells, int countdownTimer) {
Queue<Edge> queue = new PriorityQueue<Edge>(numCells, timeComparator);
Edge e = new Edge(start, 0);
queue.add(e);
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> cells = new HashSet<Integer>();
while (queue.size() > 0) {
Edge currEdge = queue.poll();
int currCell = currEdge.cell;
int currCost = currEdge.cost;
if (visited.contains(currCell)) {
continue;
}
visited.add(currCell);
if (currCost <= countdownTimer) {
if (!cells.contains(currCell)) {
cells.add(currCell);
}
}
ArrayList<Edge> reachCells = adjList.get(currCell);
for (int i = 0; i < reachCells.size(); i++) {
Edge next = reachCells.get(i);
e = new Edge(next.cell, currCost+next.cost);
queue.add(e);
}
}
return cells.size();
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
int sinal = 1;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
if (n == '-') {
sinal = -1;
}
if (n == '+') {
sinal = 1;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp*sinal;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = reader(br);
for (int i = 0; i < numCases; i++) {
int numCells = reader(br);
int exitCell = reader(br);
int countdownTimer = reader(br);
adjList = new HashMap<Integer, ArrayList<Edge>>();
for (int j = 1; j <= numCells; j++) {
adjList.put(j, new ArrayList<Edge>());
}
int numConnections = reader(br);
for (int j = 0; j < numConnections; j++) {
int a = reader(br);
int b = reader(br);
int cost = reader(br);
Edge e = new Edge(a, cost);
adjList.get(b).add(e);
}
if ((i+1) == numCases) {
System.out.println(dijkstra(exitCell, numCells, countdownTimer));
}
else {
System.out.println(dijkstra(exitCell, numCells, countdownTimer)+"\n");
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
int cell;
int cost;
public Edge(int cell, int cost) {
this.cell = cell;
this.cost = cost;
}
}
For this solution, I reversed the edges because I try to reach the maximum amount of cells from my exit cell during the count-down timer.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Edge>> adjList;
public static Comparator<Edge> timeComparator = new Comparator<Edge>() {
public int compare(Edge e1, Edge e2) {
return e1.cost - e2.cost;
}
};
public static int dijkstra(int start, int numCells, int countdownTimer) {
Queue<Edge> queue = new PriorityQueue<Edge>(numCells, timeComparator);
Edge e = new Edge(start, 0);
queue.add(e);
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> cells = new HashSet<Integer>();
while (queue.size() > 0) {
Edge currEdge = queue.poll();
int currCell = currEdge.cell;
int currCost = currEdge.cost;
if (visited.contains(currCell)) {
continue;
}
visited.add(currCell);
if (currCost <= countdownTimer) {
if (!cells.contains(currCell)) {
cells.add(currCell);
}
}
ArrayList<Edge> reachCells = adjList.get(currCell);
for (int i = 0; i < reachCells.size(); i++) {
Edge next = reachCells.get(i);
e = new Edge(next.cell, currCost+next.cost);
queue.add(e);
}
}
return cells.size();
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
int sinal = 1;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
if (n == '-') {
sinal = -1;
}
if (n == '+') {
sinal = 1;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp*sinal;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = reader(br);
for (int i = 0; i < numCases; i++) {
int numCells = reader(br);
int exitCell = reader(br);
int countdownTimer = reader(br);
adjList = new HashMap<Integer, ArrayList<Edge>>();
for (int j = 1; j <= numCells; j++) {
adjList.put(j, new ArrayList<Edge>());
}
int numConnections = reader(br);
for (int j = 0; j < numConnections; j++) {
int a = reader(br);
int b = reader(br);
int cost = reader(br);
Edge e = new Edge(a, cost);
adjList.get(b).add(e);
}
if ((i+1) == numCases) {
System.out.println(dijkstra(exitCell, numCells, countdownTimer));
}
else {
System.out.println(dijkstra(exitCell, numCells, countdownTimer)+"\n");
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Edge {
int cell;
int cost;
public Edge(int cell, int cost) {
this.cell = cell;
this.cost = cost;
}
}
Comments
Post a Comment