結果

問題 No.3482 Quod Erat Demonstrandum
コンテスト
ユーザー ルク
提出日時 2026-03-27 21:33:38
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,588 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 194 ms
コンパイル使用メモリ 85,068 KB
実行使用メモリ 189,128 KB
最終ジャッジ日時 2026-03-27 21:34:02
合計ジャッジ時間 20,121 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import heapq
from itertools import product
from itertools import permutations
import random
import math
import bisect
import sys
from collections import defaultdict
from itertools import combinations


inf = 1 << 60


def exit():
    sys.exit()


def LI():
    return list(map(int, input().split()))


def II():
    return int(input())


def SI():
    return input()


class dijkstra:
    def __init__(self, g):
        self.g = g

    def shortest_path(self, start):
        import heapq
        n = len(self.g)
        dist = [float("inf")] * n
        dist[start] = 0
        pq = [(0, start)]
        while pq:
            d, u = heapq.heappop(pq)
            if dist[u] < d:
                continue
            for v, w in self.g[u]:
                nd = d + w
                if dist[v] > nd:
                    dist[v] = nd
                    heapq.heappush(pq, (nd, v))
        return dist


t = II()
for _ in range(t):
    n, m = LI()
    items = [LI() for _ in range(m)]
    g = [[] for _ in range(n)]
    dic = defaultdict(bool)
    for item in items:
        a, b, c = item
        a, b = a-1, b-1
        if c == 1:
            g[a].append((b, 1))
            g[b].append((a, 1))
        elif c == 2:
            dic[(a, b)] = True
    dij = dijkstra(g).shortest_path(0)
    mn = inf
    for i in range(n):
        if dij[i] != float("inf") and dic[(i, n-1)]:
            mn = min(mn, dij[i]+1)
    if dij[-1] != float("inf"):
        print("Same")
        print(dij[-1])
    elif mn != inf:
        print("Different")
        print(mn)
    else:
        print("Unknown")
0