結果

問題 No.1473 おでぶなおばけさん
ユーザー harurunharurun
提出日時 2021-04-10 00:23:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,421 ms / 2,000 ms
コード長 3,317 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 70,252 KB
最終ジャッジ日時 2024-06-25 13:10:02
合計ジャッジ時間 33,274 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
19,168 KB
testcase_01 AC 73 ms
19,164 KB
testcase_02 AC 1,300 ms
64,712 KB
testcase_03 AC 722 ms
53,680 KB
testcase_04 AC 619 ms
44,984 KB
testcase_05 AC 209 ms
28,124 KB
testcase_06 AC 791 ms
55,556 KB
testcase_07 AC 1,304 ms
69,236 KB
testcase_08 AC 1,421 ms
70,252 KB
testcase_09 AC 1,291 ms
68,840 KB
testcase_10 AC 354 ms
40,984 KB
testcase_11 AC 355 ms
40,704 KB
testcase_12 AC 352 ms
41,008 KB
testcase_13 AC 227 ms
30,440 KB
testcase_14 AC 197 ms
28,640 KB
testcase_15 AC 311 ms
37,448 KB
testcase_16 AC 344 ms
40,460 KB
testcase_17 AC 88 ms
20,444 KB
testcase_18 AC 101 ms
21,724 KB
testcase_19 AC 384 ms
38,600 KB
testcase_20 AC 710 ms
49,788 KB
testcase_21 AC 589 ms
49,412 KB
testcase_22 AC 1,147 ms
60,596 KB
testcase_23 AC 909 ms
52,972 KB
testcase_24 AC 899 ms
54,100 KB
testcase_25 AC 1,129 ms
62,788 KB
testcase_26 AC 1,144 ms
65,772 KB
testcase_27 AC 368 ms
39,580 KB
testcase_28 AC 1,200 ms
66,988 KB
testcase_29 AC 438 ms
45,088 KB
testcase_30 AC 520 ms
49,484 KB
testcase_31 AC 1,295 ms
66,536 KB
testcase_32 AC 666 ms
54,436 KB
testcase_33 AC 680 ms
50,072 KB
testcase_34 AC 405 ms
37,056 KB
testcase_35 AC 307 ms
35,644 KB
testcase_36 AC 717 ms
56,168 KB
testcase_37 AC 852 ms
53,336 KB
testcase_38 AC 181 ms
27,744 KB
testcase_39 AC 350 ms
40,860 KB
testcase_40 AC 344 ms
40,752 KB
testcase_41 AC 318 ms
34,860 KB
testcase_42 AC 324 ms
34,880 KB
testcase_43 AC 579 ms
56,344 KB
testcase_44 AC 581 ms
56,340 KB
testcase_45 AC 589 ms
56,340 KB
testcase_46 AC 896 ms
57,932 KB
testcase_47 AC 1,167 ms
65,192 KB
testcase_48 AC 920 ms
60,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class dsu:
  def __init__(self,n):
    assert n>0,"n must be Natural"
    self.n=n
    self.parents=[-1]*n
    return
  
  def find(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.find(self.parents[x])
    return self.parents[x]
  
  def merge(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    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
    return
    
  def size(self,x):
    assert x<self.n,"warning!! this is 0 index"
    return -self.parents[self.find(x)]
    
  def same(self,x,y):
    assert x<self.n and y<self.n,"warning!! this is 0 index"
    return self.find(x)==self.find(y)
  
  def leader(self,x):
    assert x<self.n,"warning!! this is 0 index"
    if self.parents[x]<0:
      return x
    self.parents[x]=self.leader(self.parents[x])
    return self.parents[x]
  
  def groups(self):
    ld=[self.leader(i) for i in range(self.n)]
    res=[[] for _ in range(self.n)]
    for i in range(self.n):
      res[ld[i]].append(i)
    return list(filter(lambda a:a !=[],res))
  
from heapq import heapify,heappop,heappush
INF=1000000000000000000
V=0
G=[[] for _ in [0]*100000]
d=[INF]*100000

def dijkstra(s):
  que=[]
  heapify(que)
  d[s]=0
  heappush(que,[0,s])
  while que:
    p=heappop(que)
    v=p[1]
    if d[v]<p[0]:
      continue
    for i in range(len(G[v])):
      e=G[v][i]
      if d[e[0]]>d[v]+e[1]:
        d[e[0]]=d[v]+e[1]
        heappush(que,[d[e[0]],e[0]])
"""
G[from].append([to,value])で追加
"""
  
class INPUT:
  def __init__(self):
    self.l=open(0).read().split()[::-1]
    self.length=len(self.l)
    return

  def stream(self,k=1,f=int,f2=False):
    assert(-1<k)
    m=self.length
    if m==0 or m<k:
      raise Exception("There is no input!")
    elif f!=str:
      if k==0:
        self.length=0
        return list(map(f,self.l[::-1]))
      if k==1 and not f2:
        self.length-=1
        return f(self.l.pop())
      if k==1 and f2:
        self.length-=1
        return [f(self.l.pop())]
      ret=[]
      for _ in [0]*k:
        ret.append(f(self.l.pop()))
      self.length-=k
      return ret
    else:
      if k==0:
        self.length=0
        return self.l[::-1]
      if k==1 and not f2:
        self.length-=1
        return self.l.pop()
      if k==1 and f2:
        self.length-=1
        return [self.l.pop()]
      ret=[]
      for _ in [0]*k:
        ret.append(self.l.pop())
      self.length-=k
      return ret
pin=INPUT().stream

"""
pin(number[default:1],f[default:int],f2[default:False])
if number==0 -> return left all
listを変数で受け取るとき、必ずlistをTrueにすること。
"""

def main():
  n,m=pin(2)
  uf=dsu(n)
  ds=[pin(3)for i in range(m)]
  ds=sorted(ds,key=lambda x:x[2],reverse=True)
  w=float("inf")
  j=m-1
  for i in range(m):
    w=min(w,ds[i][2])
    uf.merge(ds[i][0]-1,ds[i][1]-1)
    G[ds[i][0]-1].append([ds[i][1]-1,1])
    G[ds[i][1]-1].append([ds[i][0]-1,1])
    if uf.same(0,n-1):
      j=i+1
      break
  while j<m and w==ds[j][2]:
    G[ds[j][0]-1].append([ds[j][1]-1,1])
    G[ds[j][1]-1].append([ds[j][0]-1,1])
    j+=1
  dijkstra(0)
  print(w,d[n-1])
  return
main()
0