Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 0b3abab

Browse files
committed
Update dining.java
1 parent fa38ff8 commit 0b3abab

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

dining.java

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import java.io.*;
22
import java.util.*;
33
public class dining {
4+
public static int[] distTo;
5+
public static int[] distOrig;
46
public static final int NO_PARENT = -1; // Constant for no parent
57
public static void main(String[] args) throws IOException {
68
BufferedReader f = new BufferedReader(new FileReader("dining.in"));
@@ -46,15 +48,66 @@ public static void main(String[] args) throws IOException {
4648
}
4749
System.out.println("Cost with haybales: "+costWithHaybales);
4850
f.close();
49-
int[] empty = new int[M];
51+
int[] empty = new int[N];
5052
Arrays.fill(empty, Integer.MAX_VALUE);
51-
int[] distTo = new int[M];
52-
distTo= Arrays.copyOf(empty, M);
53+
//distTo = new int[M];
54+
distTo= Arrays.copyOf(empty, N);
55+
distOrig= Arrays.copyOf(empty, N);
56+
Set<Integer> visited = new HashSet<>();
57+
PriorityQueue<Integer> nextNodes = new PriorityQueue<>(new Comparator<Integer>() {
58+
@Override
59+
public int compare(Integer arg0, Integer arg1) {
60+
return Integer.compare(dining.distTo[arg1], dining.distTo[arg0]);
61+
//return 0;
62+
}
63+
});
64+
nextNodes.add(N-1);
65+
// begin dijkstra from barn with haybales
66+
while(visited.size() < N) {
67+
System.out.println(visited.size());
68+
int u = nextNodes.remove(); // Get next node
69+
System.out.println(u);
70+
visited.add(u);
71+
List<Integer> adj = graph.get(u);
72+
for(int node:adj) {
73+
if(!visited.contains(node)) {
74+
int edgeDist = costWithHaybales.get(new Pair(u,node));
75+
int totalDist = distTo[u] + edgeDist;
76+
if (totalDist < distTo[node])
77+
distTo[node] = totalDist;
78+
nextNodes.add(node);
79+
}
80+
}
81+
}
82+
PriorityQueue<Integer> nextNodesOrig = new PriorityQueue<>(new Comparator<Integer>() {
83+
@Override
84+
public int compare(Integer arg0, Integer arg1) {
85+
return Integer.compare(dining.distOrig[arg1], dining.distOrig[arg0]);
86+
//return 0;
87+
}
88+
});
89+
visited.clear();
90+
nextNodesOrig.add(N-1);
91+
// begin dijkstra from barn without haybales
92+
while(visited.size() < N) {
93+
int u = nextNodesOrig.remove(); // Get next node
94+
visited.add(u);
95+
List<Integer> adj = graph.get(u);
96+
for(int node:adj) {
97+
if(!visited.contains(node)) {
98+
int edgeDist = cost.get(new Pair(u,node));
99+
int totalDist = distOrig[u] + edgeDist;
100+
if (totalDist < distOrig[node])
101+
distOrig[node] = totalDist;
102+
nextNodesOrig.add(node);
103+
}
104+
}
105+
}
106+
System.out.println("Output :" + Arrays.toString(distOrig));
107+
System.out.println("Output(haybales):" + Arrays.toString(distTo));
53108

54-
// begin dijkstra from barn
55109
}
56110
}
57-
58111
// Order does not matter pair
59112
class Pair{
60113
int x,y;

0 commit comments

Comments
 (0)