結果

問題 No.416 旅行会社
ユーザー ciel
提出日時 2016-08-27 03:26:27
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,697 ms / 4,000 ms
コード長 633 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 76,964 KB
実行使用メモリ 130,852 KB
最終ジャッジ日時 2024-12-14 20:01:52
合計ジャッジ時間 16,358 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
from collections import defaultdict
import sys,heapq

H=defaultdict(dict)
N,M,Q=map(int,sys.stdin.readline().split())
for i in range(M):
	x,y=map(int,sys.stdin.readline().split())
	x-=1;y-=1
	H[x][y]=H[y][x]=0
for i in range(Q):
	x,y=map(int,sys.stdin.readline().split())
	x-=1;y-=1
	H[x][y]=H[y][x]=Q-i

#dijkstra-like
A=[0]+[-1]*(N-1)
q=[[0,0]]
while q:
	cost1,src=heapq.heappop(q)
	for dst,cost2 in H[src].items():
		cost=max(cost1,cost2)
		if A[dst]<0 or cost<A[dst]:
			A[dst]=cost
			heapq.heappush(q,[cost,dst])

for i in range(1,N):
	if A[i]<0:
		print(0)
	elif A[i]==0:
		print(-1)
	else:
		print(Q-A[i]+1)
0