結果

問題 No.2642 Don't cut line!
ユーザー chineristACchineristAC
提出日時 2024-02-19 22:25:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 739 ms / 4,000 ms
コード長 3,069 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 157,920 KB
最終ジャッジ日時 2024-02-20 12:47:22
合計ジャッジ時間 19,005 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
58,336 KB
testcase_01 AC 666 ms
156,388 KB
testcase_02 AC 686 ms
149,472 KB
testcase_03 AC 661 ms
157,920 KB
testcase_04 AC 649 ms
157,920 KB
testcase_05 AC 619 ms
154,468 KB
testcase_06 AC 359 ms
104,948 KB
testcase_07 AC 373 ms
105,712 KB
testcase_08 AC 370 ms
105,072 KB
testcase_09 AC 366 ms
105,072 KB
testcase_10 AC 369 ms
105,580 KB
testcase_11 AC 374 ms
104,952 KB
testcase_12 AC 366 ms
105,712 KB
testcase_13 AC 369 ms
105,076 KB
testcase_14 AC 371 ms
105,200 KB
testcase_15 AC 385 ms
105,844 KB
testcase_16 AC 520 ms
111,472 KB
testcase_17 AC 573 ms
146,932 KB
testcase_18 AC 579 ms
141,172 KB
testcase_19 AC 502 ms
133,880 KB
testcase_20 AC 495 ms
113,000 KB
testcase_21 AC 349 ms
97,900 KB
testcase_22 AC 428 ms
103,552 KB
testcase_23 AC 739 ms
156,280 KB
testcase_24 AC 486 ms
111,292 KB
testcase_25 AC 488 ms
109,676 KB
testcase_26 AC 509 ms
107,372 KB
testcase_27 AC 500 ms
114,156 KB
testcase_28 AC 688 ms
138,752 KB
testcase_29 AC 534 ms
107,336 KB
testcase_30 AC 513 ms
109,304 KB
testcase_31 AC 593 ms
125,312 KB
testcase_32 AC 522 ms
108,396 KB
testcase_33 AC 44 ms
58,336 KB
testcase_34 AC 44 ms
58,336 KB
testcase_35 AC 44 ms
58,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
from heapq import heappop,heappush
from collections import deque
import random
import bisect

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)
    



def solve(N,M,C,Edge):

    Edge.sort(key=lambda e:e[2])
    uf = UnionFindVerSize(N)
    MST_edge = [[] for v in range(N)]
    total_cost = 0
    res = -1
    rest_Edge = []
    for u,v,w,p in Edge:
        if not uf.is_same_group(u,v):
            uf.unite(u,v)
            MST_edge[u].append((v,w))
            MST_edge[v].append((u,w))
            total_cost += w
            res = max(res,p)
        else:
            rest_Edge.append((u,v,w,p))

    if total_cost > C:
        return -1
    
    K = 17
    parent = [[(-1,-1)]*N for i in range(K)]
    dep = [0] * N
    deq = deque([0])
    while deq:
        v = deq.popleft()
        for nv,w in MST_edge[v]:
            if nv == parent[0][v][0]:
                continue
            parent[0][nv] = (v,w)
            dep[nv] = dep[v] + 1
            deq.append(nv)
    
    for k in range(1,K):
        for v in range(N):
            pv,nw = parent[k-1][v]
            if pv!=-1:
                ppv,nnw = parent[k-1][pv]
                parent[k][v] = (ppv,max(nnw,nw))
    
    def maxi_w_on_path(u,v):
        if dep[u] > dep[v]:
            u,v = v,u
        
        diff = dep[v] - dep[u]
        res = -1
        for k in range(K)[::-1]:
            if diff>>k & 1:
                pv,w = parent[k][v]
                res = max(res,w)
                v = pv
        
        if u == v:
            return res
        
        for k in range(K)[::-1]:
            pu,uw = parent[k][u]
            pv,vw = parent[k][v]

            if pu!=pv:
                u,v = pu,pv
                res = max(res,uw,vw)
        
        pu,uw = parent[0][u]
        pv,vw = parent[0][v]

        return max(res,uw,vw)
    
    for u,v,w,p in rest_Edge:
        tmp_cost = total_cost - maxi_w_on_path(u,v) + w
        if tmp_cost <= C:
            res = max(res,p)
    
    return res
    

N,M,C = mi()
Edge = []
for _ in range(M):
    u,v,w,p = mi()
    Edge.append((u-1,v-1,w,p))

print(solve(N,M,C,Edge))

    

    




0