結果

問題 No.1767 BLUE to RED
ユーザー tobusakanatobusakana
提出日時 2022-12-16 21:21:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,671 ms / 2,000 ms
コード長 1,068 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 82,120 KB
実行使用メモリ 313,248 KB
最終ジャッジ日時 2024-04-27 20:50:47
合計ジャッジ時間 24,441 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,884 KB
testcase_01 AC 40 ms
54,332 KB
testcase_02 AC 39 ms
53,372 KB
testcase_03 AC 40 ms
53,752 KB
testcase_04 AC 54 ms
65,164 KB
testcase_05 AC 71 ms
71,760 KB
testcase_06 AC 46 ms
60,856 KB
testcase_07 AC 95 ms
77,268 KB
testcase_08 AC 105 ms
77,672 KB
testcase_09 AC 856 ms
204,044 KB
testcase_10 AC 1,097 ms
240,084 KB
testcase_11 AC 922 ms
213,256 KB
testcase_12 AC 872 ms
205,016 KB
testcase_13 AC 1,411 ms
269,824 KB
testcase_14 AC 1,618 ms
305,960 KB
testcase_15 AC 1,609 ms
312,784 KB
testcase_16 AC 1,640 ms
305,792 KB
testcase_17 AC 1,643 ms
305,708 KB
testcase_18 AC 1,583 ms
305,600 KB
testcase_19 AC 1,620 ms
305,748 KB
testcase_20 AC 1,637 ms
305,896 KB
testcase_21 AC 1,671 ms
313,248 KB
testcase_22 AC 1,630 ms
313,016 KB
testcase_23 AC 1,627 ms
312,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# グラフ
# 頂点0を作成し、全ての赤の頂点に対して長さ0の辺を貼る
# 全ての頂点を昇順に並べ、隣合った点に距離を表す辺を貼る
# 最小全域木

N,M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

G = [[] for i in range(N + M + 1)]
for i in range(1, N + 1):
    G[0].append([i, 0])
    G[i].append([0, 0])
    
A = [[A[i], i + 1] for i in range(N)]
B = [[B[i], N + i + 1] for i in range(M)]
AB = A + B
AB = sorted(AB, key = lambda x:x[0])

for i in range(N + M - 1):
    dist = abs(AB[i + 1][0] - AB[i][0])
    to_v = AB[i + 1][1]
    from_v = AB[i][1]
    G[to_v].append([from_v, dist])
    G[from_v].append([to_v, dist])
    
import heapq as hq
q = []
hq.heappush(q, (0, 0))
visited = [False] * (N + M + 1)
ans = 0
while q:
    d, v = hq.heappop(q)
    if visited[v]:
        continue
    visited[v] = True
    ans += d
    for child, dist in G[v]:
        if visited[child]:
            continue
        hq.heappush(q, (dist, child))
        
print(ans)    







0