Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. __author__ = 'Moses Gitau'
  2.  
  3.  
  4. class FileFormat:
  5. V = 0
  6. E = 0
  7. R = 0
  8. C = 0
  9. X = 0
  10.  
  11. def __init__(self):
  12. pass
  13.  
  14.  
  15. class Video:
  16. ID = 0
  17. S = 0
  18.  
  19. def __init__(self):
  20. pass
  21.  
  22. def __repr__(self):
  23. return str(self.ID)
  24.  
  25.  
  26. class Endpoint:
  27. ID = 0
  28. LD = 0
  29. K = 0
  30. cacheServers = []
  31.  
  32. def __init__(self):
  33. pass
  34.  
  35. def __repr__(self):
  36. return str(self.ID)
  37.  
  38.  
  39. class CacheServer:
  40. ID = 0
  41. LC = 0
  42. S = 0
  43. videoList = set()
  44.  
  45. def __init__(self):
  46. pass
  47.  
  48. def __repr__(self):
  49. return str(self.ID)
  50.  
  51.  
  52. class Requests:
  53. vID = 0
  54. eID = 0
  55. rCount = 0
  56.  
  57. def __init__(self):
  58. pass
  59.  
  60. def __repr__(self):
  61. return str(self.vID)
  62.  
  63.  
  64. file = open("dataset.in", 'r')
  65. lines = file.readlines()
  66.  
  67. f = FileFormat()
  68. lineContents = lines[0].strip().split(" ")
  69. f.V = int(lineContents[0])
  70. f.E = int(lineContents[1])
  71. f.R = int(lineContents[2])
  72. f.C = int(lineContents[3])
  73. f.X = int(lineContents[4])
  74.  
  75. # read videos
  76. videos = lines[1].strip().split(" ")
  77. videoList = []
  78. index = 0
  79. for video in videos:
  80. v = Video()
  81. v.ID = int(index)
  82. v.S = int(video)
  83. videoList.append(v)
  84. index += 1
  85.  
  86. # read endpoints
  87. endPointList = []
  88. index = 2
  89. for i in range(f.E):
  90. e = Endpoint()
  91. endPoint = lines[index].strip().split(" ")
  92. e.ID = i
  93. e.LD = int(endPoint[0])
  94. e.K = int(endPoint[1])
  95. index += 1
  96. for j in range(e.K):
  97. c = CacheServer()
  98. cacheServer = lines[index].strip().split(" ")
  99. c.ID = int(cacheServer[0])
  100. c.LC = int(cacheServer[1])
  101. e.cacheServers.append(c)
  102. index += 1
  103. endPointList.append(e)
  104.  
  105. # read requests
  106. requestsList = []
  107. for i in range(index, len(lines)):
  108. r = Requests()
  109. requests = lines[i].strip().split(" ")
  110. r.vID = int(requests[0])
  111. r.eID = int(requests[1])
  112. r.rCount = int(requests[2])
  113. endPoint = endPointList[r.eID]
  114. video = videoList[r.vID]
  115. if endPoint.K is not 0 and r.rCount is not 0 and video.S < f.X:
  116. requestsList.append(r)
  117.  
  118. requestsList.sort(key=lambda x: x.rCount, reverse=True)
  119.  
  120. cacheList = {}
  121. for request in requestsList:
  122. endPoint = endPointList[request.eID]
  123. caches = endPoint.cacheServers
  124. caches.sort(key=lambda x: x.LC)
  125. video = videoList[request.vID]
  126. for cache in caches:
  127. if f.X - cache.S >= video.S:
  128. if cacheList.has_key(cache.ID):
  129. c = cacheList[cache.ID]
  130. c.videoList.add(video.ID)
  131. c.S += video.S
  132. else:
  133. cache.videoList.add(video.ID)
  134. cache.S += video.S
  135. cacheList[cache.ID] = cache
  136. break
  137.  
  138. print(len(cacheList))
  139. for cache in cacheList.values():
  140. result = str(cache.ID) + ""
  141. for video in cache.videoList:
  142. result = result + " " + str(video)
  143. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement