-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroadTrips.py
More file actions
65 lines (43 loc) · 1.35 KB
/
roadTrips.py
File metadata and controls
65 lines (43 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def roadTrip():
for _ in range (int(input)):
A = list(map(int, input().split()))
N = A[0]
M = A[1]
K = A[2]
adjMatrix = []
for _ in range(N):
adjMatrix.append([])
for _ in range(M):
curr = list(map(int, input().split()))
adjMatrix[curr[0]].append(curr[1]-1)
adjMatrix[curr[1]].append(curr[0]-1)
NList = list(map(int, input().split()))
MNList = []
for x in range(N):
MNList.append([NList[x], x])
MNList.sort()
i = 0
dones = 0
marked = []
tsum = 0
while i < (K//2):
if i not in marked:
curr = MNList[i][1]
arr = adjMatrix[curr]
tsum = tsum + curr[0]
for item in arr:
marked.append(item)
tsum = tsum + NList[item]
dones = dones + 1
if len(MNList)-i not in marked:
curr = MNList[len(MNList)-i][1]
arr = adjMatrix[curr]
tsum = tsum + curr[0]
for item in arr:
marked.append(item)
tsum = tsum + NList[item]
dones = dones + 1
if dones < K:
return -1
else:
return tsum