Wrong answer
Https://ide.codingblocks.com/s/248911
@Shlok bro what you are doing is adding just the connected components of a graph.
1
4 2
1 2 5
1 3 7
4
Now according to your code graph just contains 1-2, 2-1, 1-3, 3-1. where is 4 ??
I have made some changes but im still getting wrong answer… Can u make the appropriate changes?
Main function should look like this
public static void main(String[] args) {
Main graph = new Main();
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int v = n;
int p = 1;
while (v-- > 0) {
graph.addVertex( p);
p++;
}
while (m-- > 0) {
int x = scn.nextInt();
int y = scn.nextInt();
// graph.addVertex(x);
// graph.addVertex(y);
}
String src = scn.next();
HashMap<String, Integer> map = graph.dijkstra(src);
ArrayList keys = new ArrayList(map.keySet());
for (String key : keys) {
if (!key.equals(src)) {
if (map.get(key) == Integer.MAX_VALUE) {
System.out.print("-1 ");
continue;
} else {
System.out.print(map.get(key) + " ");
}
}
}
System.out.println()
}