結果

問題 No.1473 おでぶなおばけさん
ユーザー harurunharurun
提出日時 2021-04-10 00:37:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,172 ms / 2,000 ms
コード長 3,531 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 11,476 KB
実行使用メモリ 66,808 KB
最終ジャッジ日時 2023-09-07 19:34:22
合計ジャッジ時間 28,387 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
16,636 KB
testcase_01 AC 37 ms
16,508 KB
testcase_02 AC 1,063 ms
61,316 KB
testcase_03 AC 699 ms
49,808 KB
testcase_04 AC 511 ms
41,688 KB
testcase_05 AC 155 ms
24,800 KB
testcase_06 AC 728 ms
51,168 KB
testcase_07 AC 1,102 ms
65,732 KB
testcase_08 AC 1,172 ms
66,808 KB
testcase_09 AC 1,055 ms
65,288 KB
testcase_10 AC 339 ms
40,296 KB
testcase_11 AC 318 ms
38,740 KB
testcase_12 AC 344 ms
40,688 KB
testcase_13 AC 194 ms
25,964 KB
testcase_14 AC 145 ms
22,952 KB
testcase_15 AC 283 ms
32,168 KB
testcase_16 AC 338 ms
39,260 KB
testcase_17 AC 53 ms
17,660 KB
testcase_18 AC 65 ms
18,724 KB
testcase_19 AC 375 ms
36,152 KB
testcase_20 AC 609 ms
46,008 KB
testcase_21 AC 545 ms
47,004 KB
testcase_22 AC 877 ms
56,720 KB
testcase_23 AC 694 ms
49,944 KB
testcase_24 AC 652 ms
50,752 KB
testcase_25 AC 897 ms
58,940 KB
testcase_26 AC 929 ms
61,912 KB
testcase_27 AC 247 ms
36,404 KB
testcase_28 AC 994 ms
62,960 KB
testcase_29 AC 452 ms
42,716 KB
testcase_30 AC 487 ms
47,896 KB
testcase_31 AC 1,031 ms
63,460 KB
testcase_32 AC 538 ms
50,936 KB
testcase_33 AC 515 ms
46,860 KB
testcase_34 AC 302 ms
34,104 KB
testcase_35 AC 251 ms
32,488 KB
testcase_36 AC 561 ms
52,988 KB
testcase_37 AC 678 ms
50,184 KB
testcase_38 AC 122 ms
24,604 KB
testcase_39 AC 338 ms
39,848 KB
testcase_40 AC 345 ms
39,992 KB
testcase_41 AC 282 ms
31,240 KB
testcase_42 AC 268 ms
31,312 KB
testcase_43 AC 500 ms
52,804 KB
testcase_44 AC 518 ms
52,724 KB
testcase_45 AC 529 ms
52,800 KB
testcase_46 AC 763 ms
53,320 KB
testcase_47 AC 949 ms
61,192 KB
testcase_48 AC 784 ms
56,572 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))
  
class dijkstra:
  def __init__(self):
    self.INF=float("inf")
    self.G=[[] for _ in [0]*100000]
    self.d=[self.INF]*100000
    return
  
  def add1(self,s,t,v):
    self.G[s].append([t,v])
    return
  
  def add2(self,s,t,v):
    self.G[s].append([t,v])
    self.G[t].append([s,v])
    return

  def run(self,s):
    from heapq import heapify,heappop,heappush
    que=[]
    heapify(que)
    self.d[s]=0
    heappush(que,[0,s])
    while que:
      p=heappop(que)
      v=p[1]
      if self.d[v]<p[0]:
        continue
      for i in range(len(self.G[v])):
        e=self.G[v][i]   
        if self.d[e[0]]>self.d[v]+e[1]:
          self.d[e[0]]=self.d[v]+e[1]
          heappush(que,[self.d[e[0]],e[0]])
    return self.d


  
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
  dk=dijkstra()
  for i in range(m):
    w=min(w,ds[i][2])
    uf.merge(ds[i][0]-1,ds[i][1]-1)
    dk.add2(ds[i][0]-1,ds[i][1]-1,1)
    if uf.same(0,n-1):
      j=i+1
      break
  while j<m and w==ds[j][2]:
    dk.add2(ds[j][0]-1,ds[j][1]-1,1)
    j+=1
  d=dk.run(0)
  print(w,d[n-1])
  return
main()
0