Advertisement
Guest User

Untitled

a guest
May 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. import heapq as hq
  6. import math
  7. INF = float("inf")
  8.  
  9.  
  10.  
  11. print(math.inf, INF)
  12.  
  13. def prim(G):
  14.     first=True
  15.     firstn=-1
  16.     mayor = 0
  17.     pos =-1
  18.     n = len(G)
  19.     Known = [False]*n
  20.     #FirstTime = [-1]*n
  21.     FirstTime=[]
  22.     Cost = [math.inf]*n
  23.     Path = [-1]*n
  24.     queue = []
  25.     s = 0
  26.     totalDistance=0
  27.     Cost[s] = 0
  28.     hq.heappush(queue, (0, s))
  29.     while len(queue) > 0:
  30.         _, u = hq.heappop(queue)
  31.         if not Known[u]:
  32.             Known[u] = True
  33.             if first and u is not 0:
  34.                 firstn=u
  35.                 first=False
  36.             for v, w in G[u]:
  37.                 #if first:
  38.                     #if mayor< w:
  39.                       #  mayor=w
  40.                     #    pos=v
  41.                 if u not in FirstTime:
  42.                     if not Known[v] and w < Cost[v]:
  43.                         Cost[v] = w
  44.                         Path[v] = u
  45.                         hq.heappush(queue, (w, v))
  46.                 #if first:
  47.                # Cost[pos] = mayor
  48.               #  Path[pos] = u
  49.               #  first=False
  50.              #   hq.heappush(queue, (mayor, pos))
  51.             FirstTime.append(u)
  52.             for i in range(len(Cost)):
  53.                 _,u = queue[i]
  54.                 if u is not i:
  55.                     if u not in FirstTime:  
  56.                         Cost[i]=math.inf
  57.  
  58.     u = 0
  59.     menor=1000000
  60.     poss=-1
  61.     print(firstn)
  62.     for v, w in G[u]:
  63.         if v not in Path:
  64.             if w < menor and v is not firstn:
  65.                 menor=w
  66.                 poss=v
  67.     Cost.append(menor)
  68.     Path.append(poss)
  69.     for i in range(len(Cost)):
  70.         totalDistance=totalDistance+Cost[i]
  71.     print(totalDistance)
  72.                    
  73.     return Path, Cost
  74.  
  75.  
  76. #"Kms":3150.5989138486757 ,"recorrido": " JUNIN TUMBES LIMA ICA CUSCO JUNIN",
  77.  
  78. G = [[(1, 379.4789068684117), (2, 310.1146967131383), (3, 1108.1974979562904), (4, 334.64345985869596)], [(0, 379.4789068684117), (2, 478.2196271563756), (3, 1254.0505590111545), (4, 257.99790461704424)], [(0, 310.1146967131383), (1, 478.2196271563756), (3, 1413.7414626163775), (4, 582.0488193942442)], [(0, 1108.1974979562904), (1, 1254.0505590111545), (2, 1413.7414626163775), (4, 996.0691874058272)], [(0, 334.64345985869596), (1, 257.99790461704424), (2, 582.0488193942442), (3, 996.0691874058272)]]
  79. #with open('grafito.in') as f:
  80. #    for line in f:
  81. #        u = len(G)
  82. #        G.append([])
  83. #        nums = [int(x) for x in line.split(' ')]
  84. #        for i in range(len(nums) // 2):
  85.  #           G[u].append((nums[i * 2], nums[i * 2 + 1]))
  86.  
  87.  
  88. for i in range(len(G)):
  89.     stra =''
  90.     for j in range(len(G[i])):
  91.         stra= stra + str(G[i][j])
  92.     print(stra)
  93. print(prim(G))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement