Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-mjj111 #226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions mjj111/graph/μ½”λ“œνŠΈλ¦¬νˆ¬μ–΄.py
Original file line number Diff line number Diff line change
@@ -0,0 +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)]

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()
Loading