From 2a45c400fdc316dd8e26018f425474c00b421cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=A4=80?= <86913355+mjj111@users.noreply.github.com> Date: Sat, 14 Sep 2024 22:55:54 +0900 Subject: [PATCH 1/2] 2024-09-14 --- ...70\353\246\254\355\210\254\354\226\264.py" | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 "mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" diff --git "a/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" "b/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" new file mode 100644 index 0000000..aec60bd --- /dev/null +++ "b/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" @@ -0,0 +1,110 @@ + +import heapq +from collections import defaultdict +import itertools + +INF = float('inf') + +class TravelManager: +def __init__(self, n): +self.n = n # 도시의 수 +self.graph = defaultdict(list) # 도시 간 간선 정보 +self.distances = [INF] * n # 0번 도시부터의 최단 거리 +self.products = {} # 여행 상품 정보 (고유식별자 -> (매출, 도착지)) +self.unavailable = set() # 판매되지 않는 상품의 고유식별자 +self.heap = [] # 최적의 상품 판매를 위한 힙 + +def add_road(self, u, v, cost): +self.graph[u].append((v, cost)) +self.graph[v].append((u, cost)) # 양방향 간선 추가 + +def dijkstra(self, start): +self.distances = [INF] * self.n +self.distances[start] = 0 +pq = [(0, start)] + + while pq: +current_dist, current_node = heapq.heappop(pq) + if current_dist > self.distances[current_node]: + continue + + for neighbor, weight in self.graph[current_node]: +distance = current_dist + weight + + if distance < self.distances[neighbor]: +self.distances[neighbor] = distance + heapq.heappush(pq, (distance, neighbor)) + +def add_product(self, product_id, revenue, destination): +self.products[product_id] = (revenue, destination) +cost = self.distances[destination] + heapq.heappush(self.heap, (-(revenue - cost), product_id)) # 최대 힙 구현 + +def cancel_product(self, product_id): + if product_id in self.products: + self.unavailable.add(product_id) + +def sell_best_product(self): + while self.heap: +profit, product_id = heapq.heappop(self.heap) + + if product_id in self.unavailable: + continue # 판매하지 않는 상품은 무시 + +revenue, destination = self.products[product_id] +cost = self.distances[destination] + if cost == INF or cost > revenue: +print(-1) + return + +print(product_id) +del self.products[product_id] + return +print(-1) + +def change_departure(self, new_start): + self.dijkstra(new_start) +self.heap = [] + for product_id, (revenue, destination) in self.products.items(): + if product_id not in self.unavailable: +cost = self.distances[destination] + heapq.heappush(self.heap, (-(revenue - cost), product_id)) + + +def main(): +q = int(input()) +input_line = list(map(int, input().split())) + +n, m = input_line[1], input_line[2] +manager = TravelManager(n) +creations = input_line[3:] + + for i in range(0,len(creations),3): +creation = creations[i:i+3] +a = creation[0] +b = creation[1] +cost = creation[2] + manager.add_road(a, b, cost) + manager.dijkstra(0) + + for _ in range(q-1): +input_line = list(map(int, input().split())) +commend = input_line[0] + + if commend == 200: +product_id, revenue, destination = input_line[1], input_line[2], input_line[3] + manager.add_product(product_id, revenue, destination) + +elif commend == 300: +product_id = input_line[1] + manager.cancel_product(product_id) + +elif commend == 400: + manager.sell_best_product() + +elif commend== 500: +new_start = input_line[1] + manager.change_departure(new_start) + +if __name__ == "__main__": +main() From 58ecf6f9939c803b388d1ff8363a6f275075aeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=A4=80?= <86913355+mjj111@users.noreply.github.com> Date: Sat, 14 Sep 2024 23:01:36 +0900 Subject: [PATCH 2/2] 2024-09-14 --- ...70\353\246\254\355\210\254\354\226\264.py" | 139 +++++++++--------- 1 file changed, 70 insertions(+), 69 deletions(-) diff --git "a/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" "b/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" index aec60bd..3e6c954 100644 --- "a/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" +++ "b/mjj111/graph/\354\275\224\353\223\234\355\212\270\353\246\254\355\210\254\354\226\264.py" @@ -1,4 +1,5 @@ + import heapq from collections import defaultdict import itertools @@ -6,105 +7,105 @@ INF = float('inf') class TravelManager: -def __init__(self, n): -self.n = n # 도시의 수 -self.graph = defaultdict(list) # 도시 간 간선 정보 -self.distances = [INF] * n # 0번 도시부터의 최단 거리 -self.products = {} # 여행 상품 정보 (고유식별자 -> (매출, 도착지)) -self.unavailable = set() # 판매되지 않는 상품의 고유식별자 -self.heap = [] # 최적의 상품 판매를 위한 힙 - -def add_road(self, u, v, cost): -self.graph[u].append((v, cost)) -self.graph[v].append((u, cost)) # 양방향 간선 추가 - -def dijkstra(self, start): -self.distances = [INF] * self.n -self.distances[start] = 0 -pq = [(0, start)] + def __init__(self, n): + self.n = n # 도시의 수 + self.graph = defaultdict(list) # 도시 간 간선 정보 + self.distances = [INF] * n # 0번 도시부터의 최단 거리 + self.products = {} # 여행 상품 정보 (고유식별자 -> (매출, 도착지)) + self.unavailable = set() # 판매되지 않는 상품의 고유식별자 + self.heap = [] # 최적의 상품 판매를 위한 힙 + + def add_road(self, u, v, cost): + self.graph[u].append((v, cost)) + self.graph[v].append((u, cost)) # 양방향 간선 추가 + + def dijkstra(self, start): + self.distances = [INF] * self.n + self.distances[start] = 0 + pq = [(0, start)] while pq: -current_dist, current_node = heapq.heappop(pq) + current_dist, current_node = heapq.heappop(pq) if current_dist > self.distances[current_node]: - continue + continue + + for neighbor, weight in self.graph[current_node]: + distance = current_dist + weight - for neighbor, weight in self.graph[current_node]: -distance = current_dist + weight - if distance < self.distances[neighbor]: -self.distances[neighbor] = distance + self.distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) -def add_product(self, product_id, revenue, destination): -self.products[product_id] = (revenue, destination) -cost = self.distances[destination] + def add_product(self, product_id, revenue, destination): + self.products[product_id] = (revenue, destination) + cost = self.distances[destination] heapq.heappush(self.heap, (-(revenue - cost), product_id)) # 최대 힙 구현 -def cancel_product(self, product_id): + def cancel_product(self, product_id): if product_id in self.products: - self.unavailable.add(product_id) + self.unavailable.add(product_id) -def sell_best_product(self): + def sell_best_product(self): while self.heap: -profit, product_id = heapq.heappop(self.heap) + profit, product_id = heapq.heappop(self.heap) if product_id in self.unavailable: - continue # 판매하지 않는 상품은 무시 + continue # 판매하지 않는 상품은 무시 -revenue, destination = self.products[product_id] -cost = self.distances[destination] - if cost == INF or cost > revenue: -print(-1) + revenue, destination = self.products[product_id] + cost = self.distances[destination] + if cost == INF or cost > revenue: + print(-1) return -print(product_id) -del self.products[product_id] - return -print(-1) + print(product_id) + del self.products[product_id] + return + print(-1) -def change_departure(self, new_start): + def change_departure(self, new_start): self.dijkstra(new_start) -self.heap = [] + self.heap = [] for product_id, (revenue, destination) in self.products.items(): - if product_id not in self.unavailable: -cost = self.distances[destination] - heapq.heappush(self.heap, (-(revenue - cost), product_id)) + if product_id not in self.unavailable: + cost = self.distances[destination] + heapq.heappush(self.heap, (-(revenue - cost), product_id)) def main(): -q = int(input()) -input_line = list(map(int, input().split())) - -n, m = input_line[1], input_line[2] -manager = TravelManager(n) -creations = input_line[3:] - - for i in range(0,len(creations),3): -creation = creations[i:i+3] -a = creation[0] -b = creation[1] -cost = creation[2] - manager.add_road(a, b, cost) + q = int(input()) + input_line = list(map(int, input().split())) + + n, m = input_line[1], input_line[2] + manager = TravelManager(n) + creations = input_line[3:] + + for i in range(0,len(creations),3): + creation = creations[i:i+3] + a = creation[0] + b = creation[1] + cost = creation[2] + manager.add_road(a, b, cost) manager.dijkstra(0) for _ in range(q-1): -input_line = list(map(int, input().split())) -commend = input_line[0] + input_line = list(map(int, input().split())) + commend = input_line[0] if commend == 200: -product_id, revenue, destination = input_line[1], input_line[2], input_line[3] - manager.add_product(product_id, revenue, destination) + product_id, revenue, destination = input_line[1], input_line[2], input_line[3] + manager.add_product(product_id, revenue, destination) -elif commend == 300: -product_id = input_line[1] - manager.cancel_product(product_id) + elif commend == 300: + product_id = input_line[1] + manager.cancel_product(product_id) -elif commend == 400: - manager.sell_best_product() + elif commend == 400: + manager.sell_best_product() -elif commend== 500: -new_start = input_line[1] - manager.change_departure(new_start) + elif commend== 500: + new_start = input_line[1] + manager.change_departure(new_start) if __name__ == "__main__": -main() + main()