結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTSMasKoaTS
提出日時 2021-11-05 15:10:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 19,588 KB
最終ジャッジ日時 2024-05-01 17:31:47
合計ジャッジ時間 5,956 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 40 ms
11,008 KB
testcase_02 AC 26 ms
11,008 KB
testcase_03 AC 27 ms
11,008 KB
testcase_04 AC 27 ms
11,008 KB
testcase_05 AC 29 ms
11,008 KB
testcase_06 AC 29 ms
11,008 KB
testcase_07 AC 27 ms
11,008 KB
testcase_08 AC 27 ms
11,008 KB
testcase_09 AC 49 ms
11,008 KB
testcase_10 AC 31 ms
11,008 KB
testcase_11 AC 38 ms
11,648 KB
testcase_12 AC 56 ms
11,904 KB
testcase_13 AC 51 ms
11,776 KB
testcase_14 AC 94 ms
12,160 KB
testcase_15 AC 32 ms
11,136 KB
testcase_16 AC 46 ms
11,264 KB
testcase_17 AC 47 ms
11,392 KB
testcase_18 AC 44 ms
11,264 KB
testcase_19 AC 43 ms
11,264 KB
testcase_20 AC 120 ms
14,080 KB
testcase_21 AC 54 ms
11,776 KB
testcase_22 AC 190 ms
16,896 KB
testcase_23 AC 131 ms
14,464 KB
testcase_24 AC 216 ms
16,256 KB
testcase_25 AC 205 ms
16,256 KB
testcase_26 AC 284 ms
17,440 KB
testcase_27 AC 317 ms
17,920 KB
testcase_28 AC 317 ms
17,920 KB
testcase_29 AC 325 ms
17,920 KB
testcase_30 AC 315 ms
19,588 KB
testcase_31 AC 310 ms
19,468 KB
testcase_32 AC 78 ms
11,776 KB
testcase_33 AC 32 ms
11,264 KB
testcase_34 AC 29 ms
11,008 KB
testcase_35 AC 61 ms
12,544 KB
testcase_36 AC 55 ms
11,264 KB
testcase_37 AC 47 ms
11,904 KB
testcase_38 AC 110 ms
12,672 KB
testcase_39 AC 58 ms
11,392 KB
testcase_40 AC 59 ms
12,032 KB
testcase_41 AC 160 ms
17,152 KB
testcase_42 AC 94 ms
13,568 KB
testcase_43 AC 67 ms
11,648 KB
testcase_44 AC 122 ms
13,056 KB
testcase_45 AC 43 ms
11,392 KB
testcase_46 AC 201 ms
16,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import heapq

N,M,K = map(int,input().split())
R = set([i-1 for i in map(int,input().split())])

route = [[] for _ in [0]*N]
edge = [0]*M
st = set([0,N-1])
for i in range(M):
	A,B,C = map(int,input().split())
	A -= 1;	B -= 1
	if(i in R):
		edge[i] = [A,B,C]
		st.add(A);	st.add(B)
	route[A].append((B,C))
	route[B].append((A,C))

def dijkstra(route):
	dist = [[-1]*N for _ in [0]*N]
	for i in st:
		q = [(0,i)]
		cnt = 0
		while(q and cnt < N):
			d,v = heapq.heappop(q)
			if(dist[i][v] == -1):
				cnt += 1
				dist[i][v] = d
				for nv,nd in route[v]:
					heapq.heappush(q,(d + nd,nv))
	return dist

ans = float("inf")
rsum = sum(edge[i][2] for i in R)
dist = dijkstra(route)
for rlist in itertools.permutations(R):
	for bit in range(1 << K):
		s = rsum
		path = [0]
		for i in range(K):
			if(bit & (1 << i)):
				path.append(edge[rlist[i]][0])
				path.append(edge[rlist[i]][1])
			else:
				path.append(edge[rlist[i]][1])
				path.append(edge[rlist[i]][0])
		path.append(N-1)
		for i in range(0,len(path)-1,2):
			s += dist[path[i]][path[i+1]]
		if(ans > s):
			ans = s

print(ans)
0