QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#533061#8068. Building RoadstheodoregossettWA 52ms39208kbJava84.0kb2024-08-25 16:39:322024-08-25 16:39:32

Judging History

你现在查看的是最新测评结果

  • [2024-08-25 16:39:32]
  • 评测
  • 测评结果:WA
  • 用时:52ms
  • 内存:39208kb
  • [2024-08-25 16:39:32]
  • 提交

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'