Skip to content

Commit

Permalink
2024-09-14
Browse files Browse the repository at this point in the history
  • Loading branch information
mjj111 committed Sep 14, 2024
1 parent 2a45c40 commit 58ecf6f
Showing 1 changed file with 70 additions and 69 deletions.
139 changes: 70 additions & 69 deletions mjj111/graph/์ฝ”๋“œํŠธ๋ฆฌํˆฌ์–ด.py
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()

0 comments on commit 58ecf6f

Please sign in to comment.