From 4bb0d01188ee117b1a94e02655a0c92c55908ecc Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Sat, 30 Mar 2024 01:01:09 +0900 Subject: [PATCH] 2024-03-29 --- tgyuuAn/DP/KCM Travel.py | 30 ++++++++++++++++++++++++++++++ tgyuuAn/README.md | 1 + 2 files changed, 31 insertions(+) create mode 100644 tgyuuAn/DP/KCM Travel.py diff --git a/tgyuuAn/DP/KCM Travel.py b/tgyuuAn/DP/KCM Travel.py new file mode 100644 index 00000000..4e279062 --- /dev/null +++ b/tgyuuAn/DP/KCM Travel.py @@ -0,0 +1,30 @@ +from collections import defaultdict, deque +import sys + +def input(): return sys.stdin.readline().rstrip() + +T = int(input()) +for _ in range(T): + N, M, K = map(int, input().split()) + + costs = [[int(1e9) for _ in range(M+1)] for _ in range(N+1)] + costs[1][0] = 0 + + graph = [[] for _ in range(N+1)] + for _ in range(K): + start, destination, cost, duration = map(int,input().split()) + graph[start].append((destination, cost, duration)) + + # print(edges) + + for cost in range(M+1): + for city in range(1, N): + if costs[city][cost] == int(1e9): continue + + for now_destination, now_cost, now_duration in graph[city]: + + if now_cost + cost <= M and costs[now_destination][cost + now_cost] > costs[city][cost] + now_duration: + costs[now_destination][cost + now_cost] = costs[city][cost] + now_duration + + result = min(costs[N]) + print(result) if result != int(1e9) else print("Poor KCM") \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 5fd3dcb3..41b0c6c5 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -47,4 +47,5 @@ | 43차시 | 2024.03.10 | 이분 탐색 | 징검다리 건너기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157 | 44차시 | 2023.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 | 45차시 | 2023.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 +| 49차시 | 2023.03.29 | DP | KCM Travel | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174 ---