-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |