-
Notifications
You must be signed in to change notification settings - Fork 380
Closed
Labels
Description
LeetCode Username
shubham_math
Problem Number, Title, and Link
https://leetcode.com/problems/minimum-cost-path-with-edge-reversals/
Bug Category
Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)
Bug Description
multiples reversal are not allowed but problem accepts the code were multiple reversals are done
Language Used for Code
Java
Code used for Submit/Run operation
class Solution {
public int minCost(int n, int[][] edges) {
List<List<int[]>> graph = new ArrayList<>();
boolean[] visited = new boolean[n];
int[] minCost = new int[n];
Arrays.fill(minCost, (int)Math.pow(10,8));
for(int i = 0; i < n; i++){
graph.add(new ArrayList<>());
}
for(int[] edge : edges){
int u = edge[0];
int v = edge[1];
int cost = edge[2];
graph.get(u).add(new int[]{v, cost});
graph.get(v).add(new int[]{u, 2 * cost}); // Add Switch Too
}
// Do Dijkstra's Algorithm
minCost[0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((n1, n2) -> Integer.compare(n1[1], n2[1]));
pq.offer(new int[]{0, 0});
while(!pq.isEmpty() && !visited[n-1]){
int[] curr = pq.poll();
if(visited[curr[0]]) continue;
visited[curr[0]] = true;
for(int[] nei : graph.get(curr[0])){
if(visited[nei[0]]) continue;
int newDis = nei[1] + minCost[curr[0]];
if(minCost[nei[0]] > newDis){
minCost[nei[0]] = newDis;
pq.offer(new int[]{nei[0], newDis});
}
}
}
if(visited[n-1]) return minCost[n-1];
return -1;
}
}
Expected behavior
test case example n = 4 , edges ->{{0,2,3},{2,4,3},{4,1,1},{1,0,1},{1,2,2}} in this case ans should be 6 but my code is giving ans 4 then also the problem is submitted successfully .......
Screenshots
missing testcases
Additional context
add the test cases n = 4 , edges = {{0,2,3},{2,4,3},{4,1,1},{1,0,1},{1,2,2}};