-
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
70 additions
and
69 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 |
---|---|---|
@@ -1,110 +1,111 @@ | ||
|
||
|
||
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)] | ||
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() |