提出 #66768895


ソースコード 拡げる

# 線形基底ライブラリ
def insert_basis(basis, x):
    """10bit 線形基底へ x を挿入する(破壊的)。
       basis[i] は bit i が立つ代表ベクトル(0=空)。"""
    for bit in range(9, -1, -1):
        if not (x >> bit) & 1:
            continue
        if basis[bit]:
            x ^= basis[bit]
        else:
            basis[bit] = x
            break


def minimize_with_basis(basis, val):
    """basis で val を最小化した値を返す"""
    for bit in range(9, -1, -1):
        if basis[bit] and val > (val ^ basis[bit]):
            val ^= basis[bit]
    return val


N, M = map(int, input().split())

G = [[] for _ in range(N)]
for _ in range(M):
    A, B, W = map(int, input().split())
    A -= 1
    B -= 1
    G[A].append((B, W))

INF = float('inf')

dist = [INF] * N
dist[0] = 0

basis = [0] * 10
stack = [0]

while stack:
    u = stack.pop()
    for v, w in G[u]:
        if dist[v] == INF:
            dist[v] = dist[u] ^ w
            stack.append(v)
        else:
            cycle = dist[u] ^ w ^ dist[v]
            if cycle:
                insert_basis(basis, cycle)

if dist[N - 1] == INF:
    print(-1)
else:
    ans = minimize_with_basis(basis, dist[N - 1])
    print(ans)

提出情報

提出日時
問題 D - XOR Shortest Walk
ユーザ hirodaiten
言語 Python (PyPy 3.10-v7.3.12)
得点 0
コード長 1291 Byte
結果 WA
実行時間 71 ms
メモリ 81540 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 400
結果
AC × 3
AC × 30
WA × 3
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
hand_01.txt AC 60 ms 76692 KiB
hand_02.txt AC 59 ms 76748 KiB
hand_03.txt AC 60 ms 76648 KiB
hand_04.txt AC 60 ms 76516 KiB
hand_05.txt AC 60 ms 76416 KiB
hand_06.txt WA 60 ms 76716 KiB
hand_07.txt WA 60 ms 76384 KiB
hand_08.txt WA 59 ms 76516 KiB
random_01.txt AC 60 ms 76356 KiB
random_02.txt AC 65 ms 76564 KiB
random_03.txt AC 58 ms 76428 KiB
random_04.txt AC 62 ms 76680 KiB
random_05.txt AC 59 ms 76508 KiB
random_06.txt AC 61 ms 76380 KiB
random_07.txt AC 59 ms 76728 KiB
random_08.txt AC 65 ms 76816 KiB
random_09.txt AC 59 ms 76268 KiB
random_10.txt AC 70 ms 80860 KiB
random_11.txt AC 58 ms 76376 KiB
random_12.txt AC 66 ms 76668 KiB
random_13.txt AC 67 ms 80660 KiB
random_14.txt AC 66 ms 81192 KiB
random_15.txt AC 64 ms 80744 KiB
random_16.txt AC 59 ms 76468 KiB
random_17.txt AC 70 ms 80988 KiB
random_18.txt AC 70 ms 80820 KiB
random_19.txt AC 70 ms 80780 KiB
random_20.txt AC 71 ms 81540 KiB
random_21.txt AC 69 ms 80852 KiB
random_22.txt AC 71 ms 80628 KiB
sample_01.txt AC 61 ms 76528 KiB
sample_02.txt AC 61 ms 76408 KiB
sample_03.txt AC 60 ms 76508 KiB