結果

問題 No.92 逃走経路
ユーザー jp_stejp_ste
提出日時 2020-04-19 18:18:24
言語 JavaScript
(node v21.7.1)
結果
WA  
実行時間 -
コード長 1,520 bytes
コンパイル時間 41 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 45,056 KB
最終ジャッジ日時 2024-04-21 03:23:07
合計ジャッジ時間 2,112 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 65 ms
38,784 KB
testcase_02 AC 60 ms
38,656 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 60 ms
38,784 KB
testcase_06 AC 60 ms
38,912 KB
testcase_07 AC 63 ms
38,912 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 WA -
testcase_18 RE -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

function main(input) {
  input = input.split("\n");
  let line = input.shift().split(" ");
  const n = Number(line[0]);
  const k = Number(line[1]);
  const m = Number(line[2]);
  let list = new Array(n);
  for(let i=0; i<n; i++) {
    list[i] = new Array(n).fill().map(function(e){
      return [];
    });
  }
  for(let i=0; i<m; i++) {
    line = input.shift().split(" ");
    const p1 = Number(line[0]) - 1;
    const p2 = Number(line[1]) - 1;
    const cost = Number(line[2]);
    list[p1][p2].push(cost);
    list[p2][p1].push(cost);
  }
  let list2 = input.shift().split(" ").map(function(e) {
    return Number(e);
  });
  
  let index = 0;
  let ans = [];
  for(let i=0; i<n; i++) {
    for(let j=0; j<n; j++) {
      if(list[i][j].indexOf(list2[index]) >= 0) {
        const city = walk(i, j, list, list2, index+1);
        if(city >= 0) {
          ans.push(city);
        }
      }
    }
  }
  console.log(ans.length);
  let str = "";
  for(let i=0; i<ans.length; i++) {
    if(i > 0) str += " ";
    str += (ans[i]+1);
  }
  console.log(str);
}

function walk(from, to, list, list2, index) {
  while(true) {
    let find = false;
    from = to;    
    for(let j=0; j<list[from].length; j++) {
      const tmp = list[from][j].indexOf(list2[index]);
      if(tmp < 0) continue;
      find = true;
      to = j;
      index++;
      if(index == list2.length) {
        return to;
      }
      break;
    }
    if(!find) {
      return -1;
    }
  }
}

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