結果

問題 No.2739 Time is money
ユーザー ButterflvButterflv
提出日時 2024-04-23 20:14:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,607 ms / 2,000 ms
コード長 2,213 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 203,404 KB
最終ジャッジ日時 2024-04-23 20:14:52
合計ジャッジ時間 21,877 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,656 KB
testcase_01 AC 42 ms
54,144 KB
testcase_02 AC 308 ms
98,048 KB
testcase_03 AC 1,391 ms
182,736 KB
testcase_04 AC 306 ms
97,740 KB
testcase_05 AC 998 ms
152,808 KB
testcase_06 AC 1,410 ms
182,768 KB
testcase_07 AC 1,542 ms
196,816 KB
testcase_08 AC 1,488 ms
199,756 KB
testcase_09 AC 1,607 ms
201,588 KB
testcase_10 AC 418 ms
116,692 KB
testcase_11 AC 1,601 ms
201,428 KB
testcase_12 AC 640 ms
183,296 KB
testcase_13 AC 608 ms
183,296 KB
testcase_14 AC 517 ms
170,124 KB
testcase_15 AC 955 ms
192,584 KB
testcase_16 AC 952 ms
183,796 KB
testcase_17 AC 1,582 ms
203,404 KB
testcase_18 AC 1,525 ms
196,988 KB
testcase_19 AC 1,173 ms
184,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://note.nkmk.me/python-union-find/
from collections import defaultdict


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N, M, X=map(int, input().split())
INPUT=tuple(tuple(map(int, input().split())) for i in range(M))
uf=UnionFind(N)
for elm in INPUT:
  u, v, _, __=elm
  u-=1; v-=1
  uf.union(u, v)
if uf.same(0, N-1)==False: print(-1); exit()
del uf

edge=[[] for i in range(N)]
for elm in INPUT:
  u, v, C, T=elm
  u-=1; v-=1
  edge[u].append((v, C, T))
  edge[v].append((u, C, T))

# 連結 ⇒ 移動可能

import heapq

d=[]

timeMIN=[None for i in range(N)]
heapq.heapify(d)

# 各タプルは (時間, - 残りのお金, 頂点) で入れる
heapq.heappush(d, (0, 0, 0))

while len(d)>0:
  t, mon, v=heapq.heappop(d)
  mon*=(-1)
  if timeMIN[v]==None: timeMIN[v]=t
  else: continue
  for nv, C, T in edge[v]:
    if timeMIN[nv]!=None: continue
    nt=t+T
    nmon=mon-C
    if nmon<0:
      nt+=(abs(nmon)+X-1)//X
      nmon%=X
    heapq.heappush(d, (nt, -nmon, nv))

print(timeMIN[N-1])
0