Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(A, B, C, D) {
  2.     if(D.length === 0) return -1;
  3.     if(D[0] >= 0) return 0; // we don't need to move
  4.     if(A.length === 0 || B.length === 0 || C.length === 0) return -1;
  5.    
  6.     let isAllClosed = true;
  7.     for(let i = 0; i < D.length; i++){
  8.         if(D[i] !== -1) {
  9.             isAllClosed = false;
  10.         }
  11.     }
  12.     if(isAllClosed) return -1;
  13.    
  14.     // build graph
  15.     let graph = {}; // would adjacency matrix be better?
  16.     for(let i = 0; i < A.length; i++){
  17.         let source = A[i];
  18.         let target = B[i];
  19.         let cost = C[i];
  20.        
  21.         // one direction
  22.         if(graph[source] === undefined){ // /!\ undirect graph, must add in both directions
  23.             // one direction
  24.             graph[source] = new Map();
  25.             graph[source].set(target, cost);
  26.         } else {
  27.            let present = graph[source];
  28.            if(present.has(target)){
  29.                // did we find a shorter path for something already in there?
  30.                let currentCost = present.get(target);
  31.                if(currentCost > cost){
  32.                    graph[source].set(target, cost);
  33.                }
  34.            } else {
  35.                // path not yet present
  36.                present.set(target, cost);
  37.            }
  38.         }
  39.        
  40.         // other direction
  41.         if(graph[target] === undefined){ // /!\ undirect graph, must add in both directions
  42.             // one direction
  43.             graph[target] = new Map();
  44.             graph[target].set(source, cost);
  45.         } else {
  46.            let present = graph[target];
  47.            if(present.has(source)){
  48.                // did we find a shorter path for something already in there?
  49.                let currentCost = present.get(source);
  50.                if(currentCost > cost){
  51.                    graph[target].set(source, cost);
  52.                }
  53.            } else {
  54.                // path not yet present
  55.                present.set(source, cost);
  56.            }
  57.         }
  58.     }
  59.    
  60.    
  61.    
  62.     // at this point, we have built the graph
  63.     // we start from square 0
  64.     let visited = new Array(A.length);
  65.     visited.fill(false);
  66.  
  67.     let start = graph[0];
  68.     if(start === undefined || start.size < 1) return -1; // cannot move around
  69.    
  70.     visited[0] = true;
  71.     let queue = [];
  72.     for(let [key, value] of start){
  73.         queue.push({target: key, cost:value});
  74.     }
  75.    
  76.     while(queue.length !== 0){
  77.         let current = queue.shift();
  78.         visited[current.target] = true;
  79.        
  80.         if(D[current.target] >= current.cost){
  81.             // open shop
  82.             return current.cost;
  83.         }
  84.        
  85.         // prepare other children
  86.         let next = graph[current.target];
  87.         for(let [key, value] of next){
  88.             if(!visited[key]){
  89.                 queue.push({target: key, cost:(value+current.cost)});
  90.             }
  91.         }
  92.     }
  93.    
  94.     return -1;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement