Repository: scalekit (difficulty: hard)

Issue:
min_cost_exactly_k(cost, s, t, k) must return the minimum total weight of a directed walk from s to t that uses EXACTLY k edges (revisits allowed), or -1 if no such walk exists. cost is an n x n matrix with cost[i][j] the weight of edge i->j or -1 for no edge. The reference does a dp[node] relaxation k times, which is correct but the hidden tests use k up to 10**9, where an O(k) loop cannot finish. Re-implement so it returns the same answers in time logarithmic in k (e.g. min-plus / tropical matrix exponentiation). 'Exactly k' rules out a plain shortest-path shortcut. The public contract is unchanged for small k.

Source files:
core.py: def min_cost_exactly_k(cost, s, t, k):
    """Minimum total weight of a walk from s to t using EXACTLY k edges.

    `cost` is an n x n matrix; cost[i][j] is the weight of edge i->j, or -1 if
    no such edge exists. Walks may revisit nodes and edges. Return the minimum
    total weight over all walks of exactly k edges, or -1 if none exists.

    k may be as large as 10**9.
    """
    n = len(cost)
    INF = float("inf")
    # dp[v] = min cost to reach v using the number of edges processed so far.
    dp = [INF] * n
    dp[s] = 0
    for _ in range(k):
        ndp = [INF] * n
        for u in range(n):
            if dp[u] == INF:
                continue
            for v in range(n):
                if cost[u][v] != -1 and dp[u] + cost[u][v] < ndp[v]:
                    ndp[v] = dp[u] + cost[u][v]
        dp = ndp
    return -1 if dp[t] == INF else dp[t]


Test files:
test_core.py: import random

from core import min_cost_exactly_k


def _brute(cost, s, t, k):
    n = len(cost)
    INF = float("inf")
    dp = [INF] * n
    dp[s] = 0
    for _ in range(k):
        ndp = [INF] * n
        for u in range(n):
            if dp[u] == INF:
                continue
            for v in range(n):
                if cost[u][v] != -1 and dp[u] + cost[u][v] < ndp[v]:
                    ndp[v] = dp[u] + cost[u][v]
        dp = ndp
    return -1 if dp[t] == INF else dp[t]


def test_kedge_small_matches_brute():
    rng = random.Random(2024)
    for _ in range(150):
        n = rng.randint(1, 5)
        cost = [[-1] * n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                if rng.random() < 0.55:
                    cost[i][j] = rng.randint(0, 9)
        s = rng.randint(0, n - 1)
        t = rng.randint(0, n - 1)
        k = rng.randint(1, 12)
        assert min_cost_exactly_k(cost, s, t, k) == _brute(cost, s, t, k)


def test_kedge_no_walk_returns_minus_one():
    cost = [[-1, 5], [-1, -1]]  # only edge is 0->1; no 2-edge walk 0->1 exists
    assert min_cost_exactly_k(cost, 0, 1, 2) == -1
    assert min_cost_exactly_k(cost, 0, 1, 1) == 5


def test_kedge_huge_k_needs_matrix_power():
    # k far beyond what an O(k) loop can iterate; only logarithmic-in-k
    # matrix exponentiation returns within the harness time budget.
    cost = [[1, 2, -1], [-1, 1, 3], [4, -1, 1]]
    assert min_cost_exactly_k(cost, 0, 0, 10**9) == 1000000000
    assert min_cost_exactly_k(cost, 0, 2, 10**9 + 7) == 1000000010
    big = [[1, 7, -1, -1],
           [-1, 1, 2, -1],
           [-1, -1, 1, 3],
           [5, -1, -1, 1]]
    assert min_cost_exactly_k(big, 0, 3, 10**9) == 1000000009


Patch:
--- a/core.py
+++ b/core.py
@@ -9,16 +9,38 @@
     """
     n = len(cost)
     INF = float("inf")
-    # dp[v] = min cost to reach v using the number of edges processed so far.
-    dp = [INF] * n
-    dp[s] = 0
-    for _ in range(k):
-        ndp = [INF] * n
-        for u in range(n):
-            if dp[u] == INF:
-                continue
-            for v in range(n):
-                if cost[u][v] != -1 and dp[u] + cost[u][v] < ndp[v]:
-                    ndp[v] = dp[u] + cost[u][v]
-        dp = ndp
-    return -1 if dp[t] == INF else dp[t]
+    if k == 0:
+        return 0 if s == t else -1
+
+    base = [[INF if cost[i][j] == -1 else cost[i][j] for j in range(n)] for i in range(n)]
+
+    def min_plus(A, B):
+        C = [[INF] * n for _ in range(n)]
+        for i in range(n):
+            Ai = A[i]
+            Ci = C[i]
+            for m in range(n):
+                a = Ai[m]
+                if a == INF:
+                    continue
+                Bm = B[m]
+                for j in range(n):
+                    v = a + Bm[j]
+                    if v < Ci[j]:
+                        Ci[j] = v
+        return C
+
+    # Min-plus (tropical) matrix exponentiation: result = base^k, where the
+    # (i, j) entry of base^k is the min weight of an exactly-k-edge walk i->j.
+    result = None
+    power = base
+    e = k
+    while e:
+        if e & 1:
+            result = power if result is None else min_plus(result, power)
+        e >>= 1
+        if e:
+            power = min_plus(power, power)
+
+    ans = result[s][t]
+    return -1 if ans == INF else ans