如何仅使用Java解决有关hackerearth的最短路径问题?

时间:2019-07-18 03:12:37

标签: java shortest-path dijkstra

以下是最短路径算法的hackerearth实践部分正在测试您的理解部分的问题:- 访问here

我已经使用java尝试了所有使用过的bellman ford,floyd warshall和dijkstra算法。所有解决方案都可以通过示例测试用例,但是提交代码时我就获得了TLE。

这是我的代码:-

package learn;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class ShortestPath_Dijkstra {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); //no of vertices
        int M = sc.nextInt(); //no of edges
        List<Edge> edgeList[] = new ArrayList[M];

        // initialization of edgeList array
        for (int i = 0; i < M; i++) {
            edgeList[i] = new ArrayList<>();
        }

        boolean visited[] = new boolean[N];

        // adding edges

        for (int i = 0; i < M; i++) {
            int fromNode = sc.nextInt() - 1; //edge from vertex v1
            int toNode = sc.nextInt() - 1;   //edge to vertex v2
            int weight = sc.nextInt();       //(v1,v2) edge weight
            Edge edge = new Edge();
            edge.toNode = toNode;
            edge.weight = weight;
            edgeList[fromNode].add(edge);
        }

        // initializing dist array elements to 10^9
        int dist[] = new int[N];
        for (int i = 0; i < N; i++) {
            dist[i] = 1000000000;
        }
        dist[0] = 0;
        // Creating custom comparator for min priority queue
        Comparator<Node> distComparator = new Comparator<Node>() {

            public int compare(Node node1, Node node2) {
                // TODO Auto-generated method stub
                return node1.distVal - node2.distVal;
            }

        };

        // Creating Priority Queue
        PriorityQueue<Node> pqueue = new PriorityQueue<Node>(distComparator);
        pqueue.add(new Node(0, 0));// initializing source vertex with zero dist and zero index

        while (!pqueue.isEmpty()) {
            int vertex = pqueue.remove().vertex;
            // pqueue.remove();//remove the processed node
            if (!visited[vertex]) {
                visited[vertex] = true;// make the node visited
                for (int i = 0; i < edgeList[vertex].size(); i++) {
                    Edge edge = edgeList[vertex].get(i);
                    int toNode = edge.toNode;
                    int weight = edge.weight;
                    if (dist[toNode] > dist[vertex] + weight) {
                        dist[toNode] = dist[vertex] + weight;
                        pqueue.add(new Node(toNode, dist[toNode]));
                    }

                }

            }
        }

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

}

class Edge {
    int toNode;
    int weight;
}

class Node {
    int vertex;
    int distVal;

    public Node(int vertex, int distVal) {
        super();
        this.vertex = vertex;
        this.distVal = distVal;
    }

}

样本输入:- 5 5 1 2 5 1 3 2 3 4 1 1 4 6 3 5 5

样本输出:- 5 2 3 7

示例测试用例已通过,但是在提交实际测试用例时,我获得了TLE。

有人可以指出我的错误,也可以有人提供仅Java可用的代码。您可以在hackerearth platform上验证代码 检查它是否正常工作。

0 个答案:

没有答案