結果

問題 No.1601 With Animals into Institute
ユーザー shotoyooshotoyoo
提出日時 2021-06-24 21:24:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,483 ms / 3,000 ms
コード長 2,946 bytes
コンパイル時間 353 ms
コンパイル使用メモリ 86,976 KB
実行使用メモリ 147,284 KB
最終ジャッジ日時 2023-09-14 19:04:49
合計ジャッジ時間 23,506 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,036 KB
testcase_01 AC 79 ms
71,432 KB
testcase_02 AC 81 ms
71,348 KB
testcase_03 AC 190 ms
80,368 KB
testcase_04 AC 171 ms
80,276 KB
testcase_05 AC 183 ms
80,256 KB
testcase_06 AC 1,386 ms
141,500 KB
testcase_07 AC 1,450 ms
143,344 KB
testcase_08 AC 1,349 ms
140,976 KB
testcase_09 AC 1,417 ms
146,808 KB
testcase_10 AC 1,374 ms
146,500 KB
testcase_11 AC 1,431 ms
146,528 KB
testcase_12 AC 1,417 ms
146,536 KB
testcase_13 AC 1,349 ms
146,432 KB
testcase_14 AC 1,444 ms
146,868 KB
testcase_15 AC 1,477 ms
147,284 KB
testcase_16 AC 1,436 ms
146,884 KB
testcase_17 AC 1,483 ms
147,044 KB
testcase_18 AC 169 ms
80,256 KB
testcase_19 AC 162 ms
79,676 KB
testcase_20 AC 174 ms
80,200 KB
testcase_21 AC 170 ms
80,104 KB
testcase_22 AC 173 ms
80,348 KB
testcase_23 AC 179 ms
80,636 KB
testcase_24 AC 178 ms
80,340 KB
testcase_25 AC 185 ms
80,712 KB
testcase_26 AC 179 ms
80,396 KB
testcase_27 AC 75 ms
71,160 KB
testcase_28 AC 75 ms
71,472 KB
testcase_29 AC 77 ms
71,204 KB
testcase_30 AC 77 ms
71,424 KB
testcase_31 AC 78 ms
71,252 KB
testcase_32 AC 79 ms
71,268 KB
testcase_33 AC 78 ms
71,424 KB
testcase_34 AC 78 ms
71,196 KB
testcase_35 AC 80 ms
71,200 KB
testcase_36 AC 78 ms
71,256 KB
testcase_37 AC 78 ms
71,164 KB
testcase_38 AC 78 ms
71,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

class UF:
    # unionfind
    def __init__(self, n):
        self.n = n
        self.parent = list(range(n))
        self.size = [1] * n
    def check(self):
        return [self.root(i) for i in range(self.n)]
    def root(self, i):
        inter = set()
        while self.parent[i]!=i:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r
    def connect(self, i, j):
        # 繋いだかどうかを返す
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
            return False
        if self.size[ri]<self.size[rj]:
            self.parent[ri] = rj
            self.size[rj] += self.size[ri]
        else:
            self.parent[rj] = ri
            self.size[ri] += self.size[rj]
        return True

    
n,m = list(map(int, input().split()))
uf = UF(n)
assert all((
    2<=n<=10**5,
    1<=2*10**5,
))
ns = [[] for _ in range(n)]
s = 0
sx = 0
for i in range(m):
    a,b,c,x = map(int, input().split())
    assert all((
        1<=a<=n,
        1<=b<=n,
        a!=b,
        1<=c<=10**9,
        x in (0,1),
    ))
    sx += 1
    a -= 1
    b -= 1
    ns[a].append((c,b,x))
    ns[b].append((c,a,x))
    s += c
    uf.connect(a,b)
assert sx>0 and len(set([uf.root(i) for i in range(n)]))==1

from heapq import heappop as hpp, heappush as hp
def t2i(*vs):
    n = len(vs)
    val = 1
    nvs = [1]
    for v in vs[::-1]:
        val *= v
        nvs.append(val)
    vs = tuple(nvs[::-1])
    def to(*args):
        return sum((v1*v2 for v1,v2 in zip(vs, args)))
    def frm(val):
        res = []
        for v in vs:
            tmp, val = divmod(val, v)
            res.append(tmp)
        return res
    return to,frm
def t2i(*vs):
    n = len(vs)+1
    nvs = [1]
    val = 1
    for v in vs[::-1]:
        val *= v
        nvs.append(val)
    vs = nvs[::-1]
    tos = "def to(%s):\n" % (",".join([f"v{i}" for i in range(n)]))
    tos += "    val = 0\n"
    for i,v in enumerate(vs):
        tos += f"    val += {v} * v{i}\n"
    tos += "    return val"
    frms = f"def frm(val):\n"
    for i in range(n-1):
        frms += f"    v{i},val = divmod(val, {vs[i]})\n"
    frms += "    return %s" % ((", ".join([f"v{i}" for i in range(n-1)]) + ", val"))
    return tos, frms
tos, frms = t2i(n,2)
exec(tos)
exec(frms)
# to,frm = t2i(n,2)
inf = 2*s + 10
vals = [[inf]*n for _ in range(2)]
vals[0][n-1] = 0
q = [to(0, n-1, 0)]
while q:
    d,u,x = frm(hpp(q))
    if vals[x][u]<d:
        continue
    for c,v,xx in ns[u]:
        nx = x|xx
        nd = d+c
        if vals[nx][v]>nd:
            vals[nx][v] = nd
            hp(q, to(nd, v, nx))
ans = vals[1]
write("\n".join(map(str, ans[:n-1])))
0