Submission #66760963


Source Code Expand

Copy
'use strict'
const main = (arg) => {
const input = arg.trim().split('\n')
const [N, M] = input[0].split(' ').map(Number)
const ABW = input.slice(1).map(line => line.split(' ').map(Number))
//
const graph = Array.from({ length: N + 1 }, () => [])
for (const [a, b, w] of ABW) {
graph[a].push([b, w])
}
// BFS1XOR
const d = Array(N + 1).fill(-1)
d[1] = 0
const queue = [1]
while (queue.length) {
const u = queue.shift()
for (const [v, w] of graph[u]) {
if (d[v] === -1) {
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'use strict'

const main = (arg) => {
  const input = arg.trim().split('\n')
  const [N, M] = input[0].split(' ').map(Number)
  const ABW = input.slice(1).map(line => line.split(' ').map(Number))
  
  // グラフ構築
  const graph = Array.from({ length: N + 1 }, () => [])
  for (const [a, b, w] of ABW) {
    graph[a].push([b, w])
  }
  
  // BFSで頂点1から各頂点へのXOR距離を計算
  const d = Array(N + 1).fill(-1)
  d[1] = 0
  const queue = [1]
  while (queue.length) {
    const u = queue.shift()
    for (const [v, w] of graph[u]) {
      if (d[v] === -1) {
        d[v] = d[u] ^ w
        queue.push(v)
      }
    }
  }
  
  if(d[N] === -1){
    console.log(-1)
    return
  }
  
  // XOR基底の構築
  let basis = []
  for (const [a, b, w] of ABW) {
    if(d[a] === -1 || d[b] === -1) continue
    let cycle = d[a] ^ w ^ d[b]
    // 基底に挿入
    for (let bval of basis) {
      cycle = Math.min(cycle, cycle ^ bval)
    }
    if(cycle !== 0){
      basis.push(cycle)
      basis.sort((x, y) => y - x) // 降順ソート
    }
  }
  
  // d[N]を基底で最小化
  let ans = d[N]
  for(let bval of basis){
    ans = Math.min(ans, ans ^ bval)
  }
  
  console.log(ans)
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'))

Submission Info

Submission Time
Task D - XOR Shortest Walk
User oimo23
Language JavaScript (Node.js 18.16.1)
Score 0
Code Size 1319 Byte
Status WA
Exec Time 43 ms
Memory 44956 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 30
WA × 3
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 39 ms 42796 KiB
hand_02.txt AC 39 ms 42712 KiB
hand_03.txt AC 39 ms 42576 KiB
hand_04.txt AC 39 ms 42692 KiB
hand_05.txt AC 39 ms 42728 KiB
hand_06.txt WA 39 ms 42832 KiB
hand_07.txt WA 39 ms 42764 KiB
hand_08.txt WA 39 ms 42780 KiB
random_01.txt AC 39 ms 42728 KiB
random_02.txt AC 41 ms 43860 KiB
random_03.txt AC 39 ms 42756 KiB
random_04.txt AC 41 ms 43344 KiB
random_05.txt AC 40 ms 42752 KiB
random_06.txt AC 40 ms 43104 KiB
random_07.txt AC 39 ms 42812 KiB
random_08.txt AC 42 ms 43884 KiB
random_09.txt AC 39 ms 42848 KiB
random_10.txt AC 43 ms 44892 KiB
random_11.txt AC 39 ms 42748 KiB
random_12.txt AC 41 ms 43848 KiB
random_13.txt AC 41 ms 43204 KiB
random_14.txt AC 41 ms 43648 KiB
random_15.txt AC 40 ms 43112 KiB
random_16.txt AC 40 ms 42832 KiB
random_17.txt AC 43 ms 44956 KiB
random_18.txt AC 43 ms 44876 KiB
random_19.txt AC 42 ms 44380 KiB
random_20.txt AC 42 ms 44156 KiB
random_21.txt AC 43 ms 44952 KiB
random_22.txt AC 43 ms 44908 KiB
sample_01.txt AC 39 ms 42736 KiB
sample_02.txt AC 40 ms 42784 KiB
sample_03.txt AC 40 ms 42844 KiB


2025-06-16 (Mon)
12:32:41 +09:00