結果

問題 No.2387 Yokan Factory
ユーザー cleanttedcleantted
提出日時 2023-06-14 04:13:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,650 ms / 5,000 ms
コード長 3,654 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 65,756 KB
最終ジャッジ日時 2024-09-17 21:34:04
合計ジャッジ時間 28,679 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
14,080 KB
testcase_01 AC 49 ms
13,824 KB
testcase_02 AC 52 ms
13,952 KB
testcase_03 AC 50 ms
14,080 KB
testcase_04 AC 49 ms
13,948 KB
testcase_05 AC 48 ms
13,948 KB
testcase_06 AC 49 ms
13,952 KB
testcase_07 AC 49 ms
13,824 KB
testcase_08 AC 51 ms
14,080 KB
testcase_09 AC 51 ms
13,952 KB
testcase_10 AC 52 ms
13,952 KB
testcase_11 AC 51 ms
13,824 KB
testcase_12 AC 52 ms
14,080 KB
testcase_13 AC 50 ms
13,824 KB
testcase_14 AC 49 ms
13,824 KB
testcase_15 AC 2,423 ms
61,508 KB
testcase_16 AC 1,017 ms
65,268 KB
testcase_17 AC 3,650 ms
65,756 KB
testcase_18 AC 2,721 ms
58,980 KB
testcase_19 AC 1,724 ms
44,848 KB
testcase_20 AC 987 ms
41,820 KB
testcase_21 AC 2,625 ms
54,824 KB
testcase_22 AC 903 ms
39,140 KB
testcase_23 AC 2,896 ms
44,780 KB
testcase_24 AC 851 ms
40,904 KB
testcase_25 AC 1,091 ms
32,268 KB
testcase_26 AC 3,270 ms
49,472 KB
testcase_27 AC 1,186 ms
53,068 KB
testcase_28 AC 59 ms
14,464 KB
testcase_29 AC 70 ms
14,848 KB
testcase_30 AC 68 ms
14,848 KB
testcase_31 AC 64 ms
14,464 KB
testcase_32 AC 58 ms
14,208 KB
testcase_33 AC 55 ms
14,336 KB
testcase_34 AC 63 ms
14,720 KB
testcase_35 AC 63 ms
14,464 KB
testcase_36 AC 53 ms
14,208 KB
testcase_37 AC 51 ms
13,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import bisect
import copy
import heapq
import itertools
import math
import operator
import random
import sys
from bisect import bisect, bisect_left, bisect_right, insort
from collections import Counter, deque
from fractions import Fraction
from functools import cmp_to_key, lru_cache, partial
from inspect import currentframe
from math import ceil, gcd, log10, pi, sqrt
from typing import Iterable, Iterator, List, Tuple, TypeVar, Union

# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
# import string
# import networkx as nx
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
# mod = 10 ** 9 + 7
mod = 998244353
# mod = 1 << 128
# mod = 10 ** 30 + 1
INF = 1 << 61
DIFF = 10 ** -9
DX = [1, 0, -1, 0, 1, 1, -1, -1]
DY = [0, 1, 0, -1, 1, -1, 1, -1]

def read_values(): return tuple(map(int, input().split()))
def read_index(): return tuple(map(lambda x: int(x) - 1, input().split()))
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for _ in range(N)]
def dprint(*values): print(*values, file=sys.stderr)
def dprint2(*values):
    names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    dprint(", ".join(f"{names.get(id(value), '???')}={repr(value)}" for value in values))


class UnionFind:
    def __init__(self, N):
        self.state = [-1] * N
        self.rank = [0] * N
        self.num_group = N
    
    def get_parent(self, a):
        p = self.state[a]
        if p < 0:
            return a
        
        q = self.get_parent(p)
        self.state[a] = q
        return q

    def make_pair(self, a, b):
        pa = self.get_parent(a)
        pb = self.get_parent(b)
        if pa == pb:
            return

        if self.rank[pa] > self.rank[pb]:
            pa, pb = pb, pa
            a, b = b, a
        elif self.rank[pa] == self.rank[pb]:
            self.rank[pb] += 1

        self.state[pb] += self.state[pa]
        self.state[pa] = pb
        self.state[a] = pb
        self.num_group -= 1
    
    def is_pair(self, a, b):
        return self.get_parent(a) == self.get_parent(b)

    def get_size(self, a):
        return -self.state[self.get_parent(a)]


def validate(N, M, X, L):
    assert(1 <= N <= 2 * 10 ** 5)
    assert(1 <= M <= 2 * 10 ** 5)
    assert(1 <= X <= 10 ** 18)
    assert(len(L) == M)

    uf = UnionFind(N)
    for u, v, a, b in L:
        assert(1 <= u <= N)
        assert(1 <= v <= N)
        assert(1 <= a <= 10 ** 9)
        assert(1 <= b <= 10 ** 9)
        u -= 1
        v -= 1
        uf.make_pair(u, v)

    assert uf.get_size(0) == N
    assert uf.num_group == 1

def main():
    N, M, X = read_values() 
    L = read_lists(M)

    validate(N, M, X, L)

    G = [[] for _ in range(N)]

    for u, v, a, b in L:
        u -= 1
        v -= 1
        G[u].append((b, v, a))
        G[v].append((b, u, a))
    for i in range(N):
        G[i].sort(reverse=True)

    def f(m):
        S = [(0, 0)]
        fixed = [False] * N
        D = [INF] * N
        D[0] = 0
        while S:
            c, i = heapq.heappop(S)
            if i == N - 1:
                break
            
            if fixed[i]:
                continue
            fixed[i] = True

            for b, j, a in G[i]:
                if m > b:
                    break

                if D[j] <= c + a:
                    continue
                D[j] = c + a
                heapq.heappush(S, (c + a, j))
        
        return D[N - 1] <= X

    l = -1
    r = 10 ** 9 + 10
    while r - l > 1:
        m = (l + r) // 2
        if f(m):
            l = m
        else:
            r = m

    print(l)


if __name__ == "__main__":
    main()
0