-
Notifications
You must be signed in to change notification settings - Fork 0
/
L3_4.py
46 lines (39 loc) · 985 Bytes
/
L3_4.py
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
'''
Conectarea cu cost minim a nodurilor la mai multe surse
O(E log (M+N))
'''
import math
def dist(a,b):
return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])
n=2 #nr centrale
m=5 #nr blocuri
centrale=[(0,0), (0,4)]
blocuri=[(1,4), (1,3), (1,1), (1,0), (3,0)]
best=[-1]*m
sel=[0]*m
sol=0
conect=[(1,2), (2,3), (3,4), (4,5), (5,6), (1,6), (3,5), (5,7), (6,7)]
for i in range (n):
for j in range(m):
val=dist(centrale[i], blocuri[j])
if val<best[j] or best[j]==-1:
best[j]=val
for j in range(m):
ind=-1
for i in range(m):
if sel[i]:
continue
if ind==-1:
ind=i
continue
if best[ind]>best[i]:
ind=i
sol=sol+math.sqrt(best[ind])
sel[ind]=1
for i in range(m):
if sel[i]:
continue
val=dist(blocuri[ind], blocuri[i])
best[i]=min(best[i], val)
print(sol)
print(best)