SHARE
TWEET

metro lines

a guest Nov 14th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. data = fs.readFileSync("./metro.in", "utf-8");
  3. arr = data.split("\r\n").map(i => i.split(" ").map(k => parseInt(k)));
  4. //n,m,S,E,x,y
  5. get_to_end = ( lines, s, e) => {
  6.     if (s == e) {
  7.         return [];
  8.     } else {
  9.         cl = lines.filter(line => line[0] == s).sort((a, b) => a.length - b.length)[0];
  10.         return cl != undefined ? [cl].concat(get_to_end(lines, cl[cl.length - 1], e)) : [];
  11.     }
  12. };
  13. do_test_cases = (i) => {
  14.     if (i < arr.length) {
  15.         [s, e] = [arr[i][2], arr[i][3]];
  16.         lines = [];
  17.         for (j = i + 1; j <= i + arr[i][1]; j++)
  18.             lines.push(arr[j].slice(1, arr[j].length));
  19.         route = get_to_end(lines, s, e);
  20.         console.log(route.slice(-1)[0].slice(-1)[0] == e ? route.reduce((c, n, i) => c + (i + 1 != route.length - 1 ? 5 : 0) + ((n.length - 2) * 2), ((route[0].length - 2) * 2)) : "impossible");
  21.         do_test_cases(i + arr[i][1] + 1);
  22.     }
  23. };
  24. do_test_cases(0);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top