|
1 | 1 | import java.io.*; |
2 | 2 | import java.util.*; |
3 | 3 | 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) { |
5 | 5 | int[] dist = new int[N]; |
6 | 6 | Set<Integer> visited = new HashSet<>(); |
7 | 7 | PriorityQueue<Integer> nodeQueue = new PriorityQueue<>(new Comparator<Integer>() { |
@@ -38,29 +38,23 @@ public static void main(String[] args) throws IOException { |
38 | 38 | M = Integer.parseInt(st.nextToken()); |
39 | 39 | K = Integer.parseInt(st.nextToken()); |
40 | 40 | 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 | + } |
45 | 45 | Map<Pair,Integer> cost = new HashMap<>(); |
46 | 46 | for(int i =0 ; i < M; i ++) { |
47 | 47 | st = new StringTokenizer(f.readLine()); |
48 | 48 | int start = Integer.parseInt(st.nextToken()); |
49 | 49 | int end = Integer.parseInt(st.nextToken()); |
50 | 50 | 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 | | - } |
57 | 51 | graph.get(start-1).add(end-1); |
58 | 52 | graph.get(end-1).add(start-1); |
59 | 53 | cost.put(new Pair(start-1,end-1), edgeCost); |
60 | 54 | } |
61 | 55 | //System.out.println("Cost : "+cost); |
62 | 56 | 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); |
64 | 58 | int[] taste = new int[K]; |
65 | 59 | List<Integer> bales = new ArrayList<>(); |
66 | 60 | for(int i = 0; i < K; i ++) { |
@@ -89,11 +83,11 @@ public static void main(String[] args) throws IOException { |
89 | 83 | costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]); |
90 | 84 | } |
91 | 85 | //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); |
94 | 88 | 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)); |
97 | 91 | PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); |
98 | 92 | for(int i =0 ; i < numOfCows; i ++) { |
99 | 93 | if(distTo[i] <= distOrig[i]) { |
|
0 commit comments