結果

問題 No.2642 Don't cut line!
ユーザー chineristACchineristAC
提出日時 2024-02-19 22:25:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 648 ms / 4,000 ms
コード長 3,069 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 157,568 KB
最終ジャッジ日時 2024-09-29 03:35:06
合計ジャッジ時間 15,870 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
58,028 KB
testcase_01 AC 585 ms
155,268 KB
testcase_02 AC 572 ms
148,864 KB
testcase_03 AC 552 ms
157,568 KB
testcase_04 AC 563 ms
157,052 KB
testcase_05 AC 551 ms
153,988 KB
testcase_06 AC 327 ms
104,852 KB
testcase_07 AC 324 ms
105,884 KB
testcase_08 AC 320 ms
105,220 KB
testcase_09 AC 317 ms
105,484 KB
testcase_10 AC 347 ms
105,740 KB
testcase_11 AC 334 ms
104,976 KB
testcase_12 AC 334 ms
106,124 KB
testcase_13 AC 323 ms
105,484 KB
testcase_14 AC 328 ms
105,708 KB
testcase_15 AC 337 ms
106,112 KB
testcase_16 AC 439 ms
111,512 KB
testcase_17 AC 526 ms
146,552 KB
testcase_18 AC 516 ms
140,564 KB
testcase_19 AC 436 ms
133,732 KB
testcase_20 AC 434 ms
113,232 KB
testcase_21 AC 306 ms
98,312 KB
testcase_22 AC 386 ms
103,328 KB
testcase_23 AC 648 ms
156,052 KB
testcase_24 AC 426 ms
111,944 KB
testcase_25 AC 436 ms
109,608 KB
testcase_26 AC 459 ms
107,776 KB
testcase_27 AC 445 ms
113,732 KB
testcase_28 AC 615 ms
138,524 KB
testcase_29 AC 456 ms
107,872 KB
testcase_30 AC 437 ms
109,796 KB
testcase_31 AC 519 ms
125,212 KB
testcase_32 AC 444 ms
109,020 KB
testcase_33 AC 39 ms
57,960 KB
testcase_34 AC 40 ms
57,068 KB
testcase_35 AC 41 ms
57,504 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