QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#533061 | #8068. Building Roads | theodoregossett | WA | 52ms | 39208kb | Java8 | 4.0kb | 2024-08-25 16:39:32 | 2024-08-25 16:39:32 |
Judging History
answer
import java.util.*;
public class Main {
static class Edge implements Comparable<Edge> {
int vertex;
double weight;
public Edge(int vertex, double weight) {
this.vertex = vertex;
this.weight = weight;
}
@Override
public int compareTo(Edge other) {
return Double.compare(this.weight, other.weight);
}
}
// Method to compute the distance between two points
public static double getDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
// Prim's algorithm to build MST from a given vertex
public static List<List<Edge>> primMST(int n, int start, int[] xCoordinates, int[] yCoordinates) {
boolean[] visited = new boolean[n];
List<List<Edge>> mst = new ArrayList<>();
for (int i = 0; i < n; i++) {
mst.add(new ArrayList<>());
}
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(start, 0));
while (!pq.isEmpty()) {
Edge current = pq.poll();
int vertex = current.vertex;
if (visited[vertex]) continue;
visited[vertex] = true;
// For each unvisited adjacent vertex, add to the priority queue
for (int i = 0; i < n; i++) {
if (!visited[i]) {
double distance = getDistance(xCoordinates[vertex], yCoordinates[vertex], xCoordinates[i], yCoordinates[i]);
pq.add(new Edge(i, distance));
mst.get(vertex).add(new Edge(i, distance));
mst.get(i).add(new Edge(vertex, distance));
}
}
}
return mst;
}
// Perform BFS to find the farthest node and the distance from the starting node
public static int[] bfs(List<List<Edge>> mst, int start) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[mst.size()];
int[] dist = new int[mst.size()];
queue.add(start);
visited[start] = true;
while (!queue.isEmpty()) {
int current = queue.poll();
for (Edge edge : mst.get(current)) {
if (!visited[edge.vertex]) {
visited[edge.vertex] = true;
dist[edge.vertex] = dist[current] + 1;
queue.add(edge.vertex);
}
}
}
int maxDist = 0;
int farthestNode = start;
for (int i = 0; i < dist.length; i++) {
if (dist[i] > maxDist) {
maxDist = dist[i];
farthestNode = i;
}
}
return new int[] {farthestNode, maxDist};
}
// Method to find the diameter of the MST
public static int getTreeDiameter(List<List<Edge>> mst, int start) {
// Run BFS to find the farthest node from start
int[] result = bfs(mst, start);
// Run BFS again from the farthest node to get the diameter
result = bfs(mst, result[0]);
return result[1];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] xCoordinates = new int[n];
int[] yCoordinates = new int[n];
for (int i = 0; i < n; i++) {
xCoordinates[i] = scanner.nextInt();
yCoordinates[i] = scanner.nextInt();
}
int minDiameter = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
// Generate MST from each vertex
List<List<Edge>> mst = primMST(n, i, xCoordinates, yCoordinates);
// Find the diameter of the resulting tree
int diameter = getTreeDiameter(mst, i);
// Track the minimum diameter
minDiameter = Math.min(minDiameter, diameter);
}
System.out.println(minDiameter);
scanner.close();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 52ms
memory: 39208kb
input:
3 0 0 10 0 0 10
output:
1
result:
wrong answer 1st numbers differ - expected: '20.00000', found: '1.00000', error = '0.95000'