Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3.  
  4. class Graph:
  5.     def __init__(self):
  6.         self.ways = defaultdict(list)
  7.         self.node = set()
  8.  
  9.     def add_node(self, num):
  10.         self.node.add(num)
  11.  
  12.     def add_edge(self, num, start, finish):
  13.         self.ways[(start, finish)].append(num)
  14.  
  15.  
  16. def dekstra(n, s, graph, count):
  17.     visited = set()
  18.     length = list()
  19.     for i in range(n):
  20.         if i != s:
  21.             length.append(100500)
  22.         else:
  23.             length.append(0)
  24.     for i in range(n):
  25.         min_num = 100500
  26.         min_index = -1
  27.         for i in range(n):
  28.             if i not in visited and length[i] < min_num:
  29.                 min_index = i
  30.                 min_num = length[i]
  31.         for i in range(n):
  32.             if (min_index, i) in graph.ways:
  33.                 for q in graph.ways[(min_index, i)]:
  34.                     if int(q) + length[min_index] < length[i] \
  35.                             and count <= cups[(min_index, i)]:
  36.                         length[i] = int(q) + length[min_index]
  37.         visited.add(min_index)
  38.     return length
  39.  
  40.  
  41. def real(count):
  42.     if dekstra(n, 0, graph, count)[n - 1] <= 1440:
  43.         return True
  44.     return False
  45.  
  46.  
  47. def binary_search():
  48.     left = 0
  49.     right = 10000000
  50.     while left < right:
  51.         centre = (left + right) // 2
  52.         if real(centre):
  53.             left = centre + 1
  54.             if not real(left):
  55.                 return centre
  56.         else:
  57.             right = centre
  58.     return right
  59.  
  60.  
  61. maximum = 0
  62. cups = dict()
  63. n, m = map(int, input().split())
  64. if n == 1:
  65.     print(10000000)
  66.     exit(0)
  67. graph = Graph()
  68. for i in range(n):
  69.     graph.add_node(i)
  70. for i in range(m):
  71.     fro, to, time, count = map(int, input().split())
  72.     cups[(fro - 1, to - 1)] = (count - 3000000) // 100
  73.     cups[(to - 1, fro - 1)] = (count - 3000000) // 100
  74.     if (fro - 1 == 0 or to - 1 == 0) and maximum < (count - 3000000) // 100:
  75.         maximum = (count - 3000000) // 100
  76.     graph.add_edge(time, fro - 1, to - 1)
  77.     graph.add_edge(time, to - 1, fro - 1)
  78. print(binary_search())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement