|
1 | 1 | import java.io.*; |
2 | 2 | import java.util.*; |
3 | 3 | public class dining { |
| 4 | + public static int[] distTo; |
| 5 | + public static int[] distOrig; |
4 | 6 | public static final int NO_PARENT = -1; // Constant for no parent |
5 | 7 | public static void main(String[] args) throws IOException { |
6 | 8 | BufferedReader f = new BufferedReader(new FileReader("dining.in")); |
@@ -46,15 +48,66 @@ public static void main(String[] args) throws IOException { |
46 | 48 | } |
47 | 49 | System.out.println("Cost with haybales: "+costWithHaybales); |
48 | 50 | f.close(); |
49 | | - int[] empty = new int[M]; |
| 51 | + int[] empty = new int[N]; |
50 | 52 | 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)); |
53 | 108 |
|
54 | | - // begin dijkstra from barn |
55 | 109 | } |
56 | 110 | } |
57 | | - |
58 | 111 | // Order does not matter pair |
59 | 112 | class Pair{ |
60 | 113 | int x,y; |
|
0 commit comments