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

Commit e19a2ab

Browse files
committed
revert to adjancey list original method
1 parent 2fe0c3e commit e19a2ab

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

dining.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.io.*;
22
import java.util.*;
33
public class dining {
4-
public static int[] dijkstra(Map<Pair, Integer> cost, Map<Integer,List<Integer>> graph, final int N, int source) {
4+
public static int[] dijkstra(Map<Pair, Integer> cost, List<List<Integer>> graph, final int N, int source) {
55
int[] dist = new int[N];
66
Set<Integer> visited = new HashSet<>();
77
PriorityQueue<Integer> nodeQueue = new PriorityQueue<>(new Comparator<Integer>() {
@@ -38,29 +38,23 @@ public static void main(String[] args) throws IOException {
3838
M = Integer.parseInt(st.nextToken());
3939
K = Integer.parseInt(st.nextToken());
4040
final int numOfCows = N - 1;
41-
Map<Integer,List<Integer>> graph = new HashMap<>(M+1);
42-
//for(int i = 0; i < N+1; i ++) {
43-
// graph.add(new ArrayList<>());
44-
//}
41+
List<List<Integer>> graph = new ArrayList<>(N+1);
42+
for(int i = 0; i < N+1; i ++) {
43+
graph.add(new ArrayList<>());
44+
}
4545
Map<Pair,Integer> cost = new HashMap<>();
4646
for(int i =0 ; i < M; i ++) {
4747
st = new StringTokenizer(f.readLine());
4848
int start = Integer.parseInt(st.nextToken());
4949
int end = Integer.parseInt(st.nextToken());
5050
int edgeCost = Integer.parseInt(st.nextToken());
51-
if(!graph.containsKey(start-1)) {
52-
graph.put(start-1, new ArrayList<>());
53-
}
54-
if(!graph.containsKey(end-1)) {
55-
graph.put(end-1, new ArrayList<>());
56-
}
5751
graph.get(start-1).add(end-1);
5852
graph.get(end-1).add(start-1);
5953
cost.put(new Pair(start-1,end-1), edgeCost);
6054
}
6155
//System.out.println("Cost : "+cost);
6256
Map<Pair,Integer> costWithHaybales = new HashMap<>(cost);
63-
//System.out.println("Graph as an edgelist: "+graph);
57+
System.out.println("Graph as an edgelist: "+graph);
6458
int[] taste = new int[K];
6559
List<Integer> bales = new ArrayList<>();
6660
for(int i = 0; i < K; i ++) {
@@ -89,11 +83,11 @@ public static void main(String[] args) throws IOException {
8983
costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]);
9084
}
9185
//System.out.println("Modified New Graph : "+graph);
92-
//System.out.println("Cost : "+cost);
93-
//System.out.println("Cost with haybales: "+costWithHaybales);
86+
System.out.println("Cost : "+cost);
87+
System.out.println("Cost with haybales: "+costWithHaybales);
9488
distTo = dijkstra(costWithHaybales, graph, N+1, N);
95-
//System.out.println("Output :" + Arrays.toString(distOrig));
96-
//System.out.println("Output(haybales):" + Arrays.toString(distTo));
89+
System.out.println("Output :" + Arrays.toString(distOrig));
90+
System.out.println("Output(haybales):" + Arrays.toString(distTo));
9791
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out")));
9892
for(int i =0 ; i < numOfCows; i ++) {
9993
if(distTo[i] <= distOrig[i]) {

0 commit comments

Comments
 (0)