結果

問題 No.92 逃走経路
ユーザー jp_ste
提出日時 2020-04-20 13:49:25
言語 JavaScript
(node v23.5.0)
結果
AC  
実行時間 1,316 ms / 5,000 ms
コード長 1,862 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 47,344 KB
最終ジャッジ日時 2024-10-13 01:43:46
合計ジャッジ時間 10,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

function main(input) {
  const [n, m, k] = getIntegers(input);
  const mList = twoDimensionalArray(n, n, []);
  for(let i=0; i<m; i++) {
    const [p1, p2, cost] = getIntegers(input);
    mList[p1-1][p2-1].push(cost);
    mList[p2-1][p1-1].push(cost);
  }
  const kList = getIntegers(input);
    
  let ansList = [];
  for(let i=0; i<n; i++) {
    for(let j=0; j<n; j++) {
      if(mList[i][j].indexOf(kList[0]) >= 0) {
        ansList.pushNoSameValue(j+1);
      }
    }
  }
  for(let i=1; i<k; i++) {
    const nextK = kList[i];
    let nextAnsList = [];
    ansList.forEach(function(from) {
      for(let to=0; to<n; to++) {
        if(mList[from-1][to].indexOf(nextK) >= 0) {
          nextAnsList.pushNoSameValue(to+1);
        }
      }   
    });
    ansList = nextAnsList;
  }
  
  ansList.sortByIntegers();
  console.log(ansList.length);
  console.log(ansList.oneLineString());
}

//my functions ------------------------------------

function getIntegers(lines) {
  return lines.shift().split(" ").map(function(e) {
    return Number(e);
  });
}

function getStrings(lines) {
  return lines.shift().split(" ");
}

function twoDimensionalArray(h, w, value) {
  const list = new Array(h);
  for(let i=0; i<h; i++) {
    list[i] = new Array(w).fill().map(function(e) {
      return (typeof value) == "object" ? Object.create(value) : value;
    });
  }
  return list;
}

Array.prototype.pushNoSameValue = function(...values) {
  values.forEach(function(e) {
    if(!this.includes(e)) {
      this.push(e);
    }
  }, this);
};

Array.prototype.sortByIntegers = function() {
  this.sort(function(a, b) {
    return a - b;
  });
};

Array.prototype.oneLineString = function() {
  let str = "";
  this.forEach(function(e, i) {
    if(i > 0) str += " ";
    str += e;
  });
  return str;
};

main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"));
0