Cod sursa(job #2230003)

Utilizator radustn92Radu Stancu radustn92 Data 8 august 2018 18:18:08
Problema Algoritmul lui Dijkstra Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 4.63 kb
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("dijkstra.in");
        OutputStream outputStream = new FileOutputStream("dijkstra.out");

        try (InputReader inputReader = new InputReader(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream)) {
            int numberNodes = inputReader.nextInt();
            int numberEdges = inputReader.nextInt();
            Graph graph = new Graph(numberNodes);
            for (int edge = 0; edge < numberEdges; edge++) {
                int from = inputReader.nextInt();
                int to = inputReader.nextInt();
                int cost = inputReader.nextInt();
                graph.addEdge(from, to, cost);
            }

            int[] distancesFromNodeOne = Dijkstra.runFromNode(1, graph);
            for (int i = 2; i <= numberNodes; i++) {
                printWriter.print(distancesFromNodeOne[i] + " ");
            }
        }
    }

    static class Dijkstra {

        private static final Integer INF = Integer.MAX_VALUE >> 1;

        public static int[] runFromNode(int source, Graph graph) {
            int[] costToNode = new int[graph.getNumberOfNodes() + 1];
            Arrays.fill(costToNode, INF);

            PriorityQueue<HeapEntry> candidateNodes = new PriorityQueue<>();
            candidateNodes.add(new HeapEntry(source, 0));
            while (!candidateNodes.isEmpty()) {
                HeapEntry heapEntry = candidateNodes.poll();
                // there may some outdated estimates for nodes
                if (costToNode[heapEntry.node] < heapEntry.cost)  {
                    continue;
                }

                costToNode[heapEntry.node] = heapEntry.cost;
                for (Graph.Edge edge : graph.getEdges(heapEntry.node)) {
                    int newEstimate = heapEntry.cost + edge.cost;
                    if (costToNode[edge.to] > newEstimate) {
                        costToNode[edge.to] = newEstimate;
                        candidateNodes.add(new HeapEntry(edge.to, newEstimate));
                    }
                }
            }

            // map unreachable nodes to 0 (required by the caller)
            for (int i = 1; i <= graph.getNumberOfNodes(); i++) {
                if (costToNode[i] == INF) {
                    costToNode[i] = 0;
                }
            }

            return costToNode;
        }

        static class HeapEntry implements Comparable<HeapEntry> {
            private int node;

            private int cost;

            public HeapEntry(int node, int cost) {
                this.node = node;
                this.cost = cost;
            }

            @Override
            public int compareTo(HeapEntry o) {
                return Integer.compare(cost, o.cost);
            }
        }
    }

    static class Graph {

        private int N;

        private List<List<Edge>> edges;

        public Graph(int numberOfNodes) {
            this.N = numberOfNodes;
            edges = new ArrayList<>(N + 1);
            for (int node = 0; node <= N; node++) {
                edges.add(new LinkedList<>());
            }
        }

        public void addEdge(int from, int to, int cost) {
            edges.get(from).add(new Edge(to, cost));
        }

        public int getNumberOfNodes() {
            return N;
        }

        public List<Edge> getEdges(int node) {
            return edges.get(node);
        }

        static class Edge {
            private int to;

            private int cost;

            public Edge(int to, int cost) {
                this.to = to;
                this.cost = cost;
            }
        }
    }

    static class InputReader implements AutoCloseable {

        private BufferedReader bufferedReader;

        private StringTokenizer stringTokenizer;

        public InputReader(InputStream inputStream) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            stringTokenizer = null;
        }

        private String nextToken() {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                try {
                    stringTokenizer = new StringTokenizer(bufferedReader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            return stringTokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        @Override
        public void close() throws IOException {
            bufferedReader.close();
        }
    }
}