Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import sys, math
  2.  
  3. class Node(object):
  4. '''Class representing a node of the network, each node as :
  5. a unique id, constant
  6. a list of neighbour nodes'''
  7. def __init__(self, n =0):
  8. self.n = n
  9. self.neighb = []
  10.  
  11. def addNeighb(self, m):
  12. self.neighb.append(m)
  13.  
  14. def removeNeighb(self, m):
  15. self.neighb.remove(m)
  16.  
  17. class Path(object):
  18. '''class representing a path in the network, by a list of node'''
  19. def __init__(self, begin, end, nodes):
  20. '''This constructor builds a shortest path in the network described by
  21. nodes, between begin and end'''
  22. self.p = []
  23. N = len(nodes)
  24.  
  25. # explored[i] is True <=> node i has been visited
  26. # prec[i] = j <=> node i has been visited coming from node j
  27. explored, prec = [], []
  28. for i in range(N):
  29. explored.append(False)
  30. prec.append(-1)
  31. explored[begin.n] = True
  32.  
  33. # A BFS is performed starting from node begin, until finding node end
  34. q, n = [], None
  35. q.append(begin)
  36. while len(q) != 0:
  37. n = q.pop()
  38. if n == end:
  39. break
  40. else:
  41. for m in n.neighb:
  42. if not explored[m.n]:
  43. explored[m.n] = True
  44. prec[m.n] = n
  45. q[0:0] = [m]
  46. # if the queue is empty before finding end, there is no path
  47. # else a shortest path is identified by backtracking
  48. if n == end:
  49. while n != begin:
  50. self.p[0:0] = [n]
  51. n = prec[n.n]
  52. self.p[0:0] = [n]
  53.  
  54. def length(self):
  55. return len(self.p)
  56.  
  57. def addFirst(self, n):
  58. self.p[0:0] = [n]
  59.  
  60. def getNode(self, i):
  61. return self.p[i]
  62.  
  63. def cut(n1, n2):
  64. '''removes the edge between node n1 and n2'''
  65. n1.removeNeighb(n2)
  66. n2.removeNeighb(n1)
  67. print("{} {}".format(n1.n, n2.n))
  68.  
  69. # number of nodes, links and exits
  70. N, L, E = [int(i) for i in input().split()]
  71.  
  72. # initializing the array of nodes and linking them
  73. nodes = []
  74. for i in range(N):
  75. nodes.append(Node(i))
  76. for i in range(L):
  77. N1, N2 = [int(j) for j in input().split()]
  78. nodes[N1].addNeighb(nodes[N2])
  79. nodes[N2].addNeighb(nodes[N1])
  80.  
  81. # initializing the array of exits
  82. exits = []
  83. for i in range(E):
  84. exits.append(nodes[int(input())])
  85.  
  86. while 1:
  87. # each turn, identifies one of the shortest paths from Skynet Agent
  88. # to each exit node (by a Breadth First Search), then cuts the shortest
  89. SI = int(input())
  90. shortestPaths = []
  91. minLength = 9999999
  92. pathToCut = None
  93.  
  94. for i in range(E):
  95. shortestPaths.append(Path(nodes[SI], exits[i], nodes))
  96. length = shortestPaths[i].length()
  97. if (length != 0) and (length < minLength):
  98. minLength = length
  99. pathToCut = shortestPaths[i]
  100.  
  101. cut(pathToCut.getNode(0), pathToCut.getNode(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement