Cod sursa(job #1741805)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 15 august 2016 10:39:11
Problema Algoritmul lui Dijkstra Scor 100
Compilator java Status done
Runda Arhiva educationala Marime 4.97 kb
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(new FileInputStream("dijkstra.in"));

        int N = in.nextInt();
        int M = in.nextInt();

        WeightedGraph graph = new WeightedGraph(N);

        for (int i = 0; i < M; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int w = in.nextInt();

            graph.addEdge(x, y, w);
        }

        int[] distance = new DijkstraAlgorithm(graph).computeDistances(1);

        for (int i = 0; i < N; i++) {
            if (distance[i] == Integer.MAX_VALUE)
                distance[i] = 0;
        }

        PrintWriter out = new PrintWriter(new FileOutputStream("dijkstra.out"));

        for (int i = 1; i < N; i++) {
            out.print(distance[i] + " ");
        }

        out.println();
        out.close();
    }

    static class WeightedGraph{
        private class Edge{
            int node;
            int cost;
            int next;

            Edge(int node, int cost, int next){
                this.node = node;
                this.cost = cost;
                this.next = next;
            }
        }

        final static int NIL = -1;
        private int[] head;
        private ArrayList<Edge> graph;

        private int N;
        private int counter;

        WeightedGraph(int N){
            initialize(N);
        }

        public int getN() {
            return N;
        }

        private void initialize(final int N){
            head = new int[N];
            graph = new ArrayList<>();

            this.N = N;
            this.counter = 0;

            Arrays.fill(head, NIL);
        }

        void addEdge(int x, int y, int w){
            if (!(1 <= x && x <= N)) throw new AssertionError();
            x--; y--;
            graph.add(new Edge(y, w, head[x]));
            head[x] = counter++;
        }

        int getHead(int node){
            if (!(1 <= node && node <= N)) throw new AssertionError();
            node--;
            return head[node];
        }

        int getNext(int p){
            if (!(0 <= p && p < counter)) throw new AssertionError();
            return graph.get(p).next;
        }

        int getNeighbour(int p){
            if (!(0 <= p && p < counter)) throw new AssertionError();
            return graph.get(p).node + 1;
        }

        int getCost(int p){
            if (!(0 <= p && p < counter)) throw new AssertionError();
            return graph.get(p).cost;
        }
    }

    static class Node implements Comparable<Node>{
        int distance;
        int node;

        public Node(int distance, int node) {
            this.distance = distance;
            this.node = node;
        }

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

    static class DijkstraAlgorithm {
        private final WeightedGraph weightedGraph;
        private final int[] distance;
        private final int N;

        DijkstraAlgorithm(WeightedGraph WG){
            weightedGraph = WG;
            this.N = WG.getN();
            distance = new int[N];
        }

        int[] computeDistances(int source){
            Arrays.fill(distance, Integer.MAX_VALUE);
            Queue<Node> minHeap = new PriorityQueue<>();

            source--;
            distance[source] = 0;
            minHeap.add(new Node(0, source));

            while (!minHeap.isEmpty()){
                Node topNode = minHeap.remove();
                int node = topNode.node;

                if (distance[node] != topNode.distance)
                    continue;

                for (int p = weightedGraph.getHead(node + 1); p != WeightedGraph.NIL; p = weightedGraph.getNext(p)) {
                    int son = weightedGraph.getNeighbour(p) - 1;
                    int cost = weightedGraph.getCost(p);

                    if (distance[son] > distance[node] + cost){
                        distance[son] = distance[node] + cost;
                        minHeap.add(new Node(distance[son], son));
                    }
                }
            }

            return distance;
        }
    }


    static class InputReader{
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 65536);
            tokenizer = null;
        }

        private String nextToken() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()){
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
                catch (IOException e){
                    throw new RuntimeException(e);
                }
            }

            return  tokenizer.nextToken();
        }

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

        String nextString(){
            return nextToken();
        }
    }
}