This commit is contained in:
nik
2025-10-01 22:55:09 +03:00
parent 4d0ece634b
commit 74e98c37c4
591 changed files with 20286 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
def greedy_change(n, coins):
result = []
for value, count in sorted(coins, key=lambda x: -x[0]):
while count > 0 and n >= value:
n -= value
count -= 1
result.append(value)
return result if n == 0 else None
if __name__ == "__main__":
coins = [(10, 3), (5, 2), (2, 5), (1, 10)]
N = 28
print(greedy_change(N, coins))

View File

@@ -0,0 +1,20 @@
def greedy_thief(weights, values, M, K):
items = list(zip(weights, values))
items.sort(key=lambda x: x[1] / x[0], reverse=True)
cap = M * K
total_value = 0
chosen = []
for w, v in items:
if w <= cap:
chosen.append((w, v))
cap -= w
total_value += v
return total_value, chosen
if __name__ == "__main__":
weights = [2, 3, 4, 5, 9, 7, 3]
values = [3, 4, 5, 8, 10, 7, 6]
M = 2
K = 10
print(greedy_thief(weights, values, M, K))

View File

@@ -0,0 +1,26 @@
import heapq
def dijkstra(graph, start):
dist = {v: float("inf") for v in graph}
dist[start] = 0
pq = [(0, start)]
while pq:
d, v = heapq.heappop(pq)
if d > dist[v]:
continue
for u, w in graph[v]:
if dist[v] + w < dist[u]:
dist[u] = dist[v] + w
heapq.heappush(pq, (dist[u], u))
return dist
if __name__ == "__main__":
graph = {
"Невский": [("Лиговский", 5), ("Гороховая", 3)],
"Лиговский": [("Московский", 7)],
"Гороховая": [("Московский", 4)],
"Московский": [],
}
print(dijkstra(graph, "Невский"))