結果

問題 No.1602 With Animals into Institute 2
ユーザー shotoyooshotoyoo
提出日時 2021-07-03 01:07:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 6,155 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 13,568 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-07-02 02:09:40
合計ジャッジ時間 3,790 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
権限があれば一括ダウンロードができます

ソースコード

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 main2(ns,es,k):
    """サイクルだけでACするやつ
    """
    inf = 10**12
    n = len(ns)
    ans = [inf]*(n-1)
    for s in range(n-1):
        x = d = 0
        for i in range(s,n-1):
            cc,xx,_ = dd[i,i+1]
            x ^= xx
            d += cc
        if x!=0:
            ans[s] = min(ans[s], d)
        x = d = 0
        for i in range(s,-1,-1):
            cc,xx,_ = dd[i,(i-1)%n]
            x ^= xx
            d += cc
        if x!=0:
            ans[s] = min(ans[s], d)
    for i in range(n-1):
        if ans[i]==inf:
            ans[i] = -1
    return ans
def main3(ns,es,k):
    """一般マッチングを解いて、小さいケースならACできるやつ
    """
    inf = 10**18
    n = len(ns)
    ans = [inf]*(n-1)
    import networkx as nx
    def add(u,v,ww):
        if (u,v) in g.edges:
            w = g[u][v]["weight"]
            w *= -1
            if ww<w:
                g.add_edge(u,v,weight=-ww)
        else:
            g.add_edge(u,v,weight=-ww)
    for s in range(n-1):
        val = inf
        for i in range(k):
            g = nx.Graph()
            for u in range(2*n):
                g.add_node(u)
            for u,v,c,x in es:
                vv = x>>i&1
                if not vv:
                    add(u,n+v,c)
                    add(v,n+u,c)
                else:
                    add(u,v,c)
                    add(n+u,n+v,c)
            for u in range(n-1):
                if u==s:
                    continue
                add(u,n+u,0)
            g.remove_node(n+s)
            g.remove_node(2*n-1)
            res = nx.max_weight_matching(g, maxcardinality=True)
            if len(res)<n-1:
                continue
            val = min(val, -sum((g[u][v]["weight"] for u,v in res)))
        if val==inf:
            val = -1
        ans[s] = val
    return ans
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 n<100
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)
ans2 = main3(ns,es,k)
assert ans==ans2
write("\n".join(map(str, ans)))
0