結果

問題 No.1602 With Animals into Institute 2
ユーザー shotoyooshotoyoo
提出日時 2021-07-03 00:29:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,055 ms / 4,000 ms
コード長 4,224 bytes
コンパイル時間 1,299 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 233,428 KB
最終ジャッジ日時 2023-09-14 19:09:16
合計ジャッジ時間 41,702 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,472 KB
testcase_01 AC 76 ms
71,172 KB
testcase_02 AC 78 ms
71,160 KB
testcase_03 AC 193 ms
81,772 KB
testcase_04 AC 172 ms
80,808 KB
testcase_05 AC 304 ms
84,196 KB
testcase_06 AC 1,660 ms
210,024 KB
testcase_07 AC 1,615 ms
208,720 KB
testcase_08 AC 2,886 ms
229,524 KB
testcase_09 AC 2,906 ms
232,456 KB
testcase_10 AC 2,953 ms
233,256 KB
testcase_11 AC 2,969 ms
232,368 KB
testcase_12 AC 2,943 ms
232,276 KB
testcase_13 AC 2,933 ms
232,032 KB
testcase_14 AC 2,960 ms
231,416 KB
testcase_15 AC 2,835 ms
231,484 KB
testcase_16 AC 2,842 ms
231,768 KB
testcase_17 AC 3,055 ms
233,428 KB
testcase_18 AC 303 ms
84,404 KB
testcase_19 AC 292 ms
84,116 KB
testcase_20 AC 297 ms
84,092 KB
testcase_21 AC 317 ms
84,452 KB
testcase_22 AC 299 ms
84,124 KB
testcase_23 AC 304 ms
83,908 KB
testcase_24 AC 291 ms
82,144 KB
testcase_25 AC 305 ms
82,948 KB
testcase_26 AC 305 ms
84,268 KB
testcase_27 AC 78 ms
71,060 KB
testcase_28 AC 75 ms
71,040 KB
testcase_29 AC 75 ms
70,920 KB
testcase_30 AC 77 ms
71,104 KB
testcase_31 AC 76 ms
71,124 KB
testcase_32 AC 76 ms
71,028 KB
testcase_33 AC 75 ms
71,008 KB
testcase_34 AC 78 ms
71,216 KB
testcase_35 AC 78 ms
70,924 KB
testcase_36 AC 77 ms
71,216 KB
testcase_37 AC 77 ms
70,864 KB
testcase_38 AC 78 ms
71,168 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, depth):
        self.n = n
        self.depth = depth
        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.depth[rj]<self.depth[ri]:
            self.parent[ri] = rj
            self.size[rj] += self.size[ri]
        else:
            self.parent[rj] = ri
            self.size[ri] += self.size[rj]
        return True

def main(ns, es,k):
    inf = 10**18
    n = len(ns)
    m = len(es)
    depth = [0]*n
    ans = [-1]*n
    h = []
    q = [inf]*n
    parent = list(range(n))
    uf = UF(n, depth)
    d = [-1]*n
    l = [-1]*n

    ### ダイクストラ dijkstra
    _val = 1<<20
    def to(d,u):
        return d*_val+u
    def frm(val):
        return divmod(val,_val)
    def dijkstra(start,ns):
        import heapq
        vals = [inf] * n
        h = [to(0, start)] # (距離, ノード番号)
        vals[start] = 0
        ps = [-1]*n
        ps[start] = (-1,-1,start)
        # order = []
        while h:
            val, u = frm(heapq.heappop(h))
            if val>vals[u]:
                continue
            # order.append(u)
            for d,x,e,v in ns[u]:
                if vals[v]>val+d:
                    vals[v] = val+d
                    heapq.heappush(h, to(vals[v], v))
                    ps[v] = (d,x,u)
        return vals, ps

    vals, ps = dijkstra(n-1, ns)
    ns2 = [[] for _ in range(n)]
    for i,(dd,x,v) in enumerate(ps[:-1]):
        ns2[i].append((dd,x,v))
        ns2[v].append((dd,x,i))
    qq = [n-1]
    d[n-1] = 0
    l[n-1] = 0
    while qq:
        u = qq.pop()
        for dd,x,v in ns2[u]:
            if d[v]>=0:
                continue
            d[v] = d[u] + dd
            l[v] = l[u] ^ x
            depth[v] = depth[u] + 1
            qq.append(v)
    from heapq import heappop as hpp, heappush as hp, heapify
    hbest = [inf]*m
    for e,(u,v,c,x) in enumerate(es):
        if l[u]^x!=l[v]:
            h.append((d[u]+d[v]+c, e))
    heapify(h)
    while h:
        val, e = hpp(h)
        if val>=inf:
            break
        if val>hbest[e]:
            continue
        hbest[e] = val
        u,v,c,x = es[e]
        w1 = uf.root(u)
        w2 = uf.root(v)
        b = []
        while w1!=w2:
            if depth[w1]<=depth[w2]:
                b.append(w2)
                w2 = uf.root(ps[w2][2])
            else:
                b.append(w1)
                w1 = uf.root(ps[w1][2])
    #     print(w1,w2)
        for ww in b:
            uf.connect(w1,ww)
            # q[ww] = min(q[ww], val - d[w1])
            q[ww] = val - d[ww]
            for cc,xx,ee,vv in ns[ww]:
                val2 = q[ww] + d[vv] + cc
                if val2<hbest[ee]:
                    hp(h, (val2, ee))
    for v in range(n-1):
        if l[v]!=0:
            ans[v] = d[v]
        else:
            ans[v] = q[v]
        if ans[v]==inf:
            ans[v] = -1
    return ans[:-1]
# グラフの読み込み・重みあり
inf = 10**16
n,m,k = map(int, input().split())
assert all((
    2<=n<=10**5,
    1<=m<=2*10**5,
    1<=k<=30,
))
ns = [[] for _ in range(n)]
es = []
for e in range(m):
    u,v,c,x = input().split()
    u = int(u)
    v = int(v)
    c = int(c)
    assert all((
        1<=u<=n,
        1<=v<=n,
        u!=v,
        1<=c<=10**9,
        len(x)==k,
        all((xx in("0","1") for xx in x))
    ))
    x = int(x, 2)
    u -= 1
    v -= 1
    ns[u].append((c,x,e,v))
    ns[v].append((c,x,e,u))
    es.append((u,v,c,x))
ans = main(ns,es,k)
write("\n".join(map(str, ans)))
0