amphora's picture
v2: add 216 optimize tasks / 77 families (Tao constants, AlphaEvolve, Leaps in Bounds, FrontierShift, FrontierCO MDS, ALE-Bench, FrontierCS, EdgeBench)
ac7c4a9 verified
Raw History Blame Contribute Delete
3.07 kB
"""ALE-Bench ahc017 (AHC 017 "Road Repair"): Python port of the official tools/src/lib.rs (parse_output +
compute_score). All-pairs shortest paths per day via scipy's Dijkstra (float64 holds the integer path lengths,
all <= 1e9 < 2^53, exactly); unreachable pairs count as 1e9 as in the tool. The final division replicates
Rust's `num as f64 / den as f64 * 1000.0` and f64::round. Answer = the contestant's stdout text."""
import math
import re
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
from run import Invalid
INF = 1_000_000_000
_UINT = re.compile(r"\+?[0-9]+\Z") # Rust usize::from_str: optional '+', ASCII digits
_WS = re.compile(r"[\t\n\x0b\x0c\r ]+")
MAX_LEN = 200_000
def rust_round(x):
r = math.floor(x)
return r + 1 if x - r >= 0.5 else r
def parse_input(text):
t = [int(x) for x in text.split()]
N, M, D, K = t[:4]
es = [(t[4 + 3 * i] - 1, t[5 + 3 * i] - 1, t[6 + 3 * i]) for i in range(M)]
return N, M, D, K, es
def apsp(N, es, mask):
best = {}
for (u, v, w), keep in zip(es, mask):
if keep:
a, b = (u, v) if u < v else (v, u)
if (a, b) not in best or w < best[(a, b)]:
best[(a, b)] = w
if best:
rows, cols, ws = zip(*[(a, b, w) for (a, b), w in best.items()])
else:
rows, cols, ws = (), (), ()
g = csr_matrix((np.array(ws, dtype=np.float64), (np.array(rows, dtype=np.int64), np.array(cols, dtype=np.int64))),
shape=(N, N))
d = dijkstra(g, directed=False)
d[~np.isfinite(d)] = INF
return d.astype(np.int64)
def check(inst, ans):
out = ans.get("output")
if not isinstance(out, str):
raise Invalid("output must be a string")
if len(out) > MAX_LEN:
raise Invalid(f"output longer than {MAX_LEN} characters")
if not out.isascii():
raise Invalid("output must be ASCII")
N, M, D, K, es = parse_input(inst["input"])
tokens = [t for t in _WS.split(out) if t]
r = []
for k in range(M):
if k >= len(tokens):
raise Invalid("Unexpected EOF")
tk = tokens[k]
if not _UINT.match(tk) or len(tk) > 25:
raise Invalid(f"Parse error: {tk[:20]}")
v = int(tk)
if v >= 2 ** 64:
raise Invalid(f"Parse error: {tk[:20]}")
if v < 1 or D < v:
raise Invalid(f"Out of range: {v}")
r.append(v)
if len(tokens) > M:
raise Invalid("Too many outputs")
count = [0] * (D + 1)
for x in r:
count[x] += 1
for i in range(1, D + 1):
if count[i] > K:
raise Invalid(f"The number of edges to be repaired on day {i} has exceeded the limit. ({count[i]} > {K})")
iu = np.triu_indices(N, 1)
d0 = apsp(N, es, [True] * M)[iu]
num = 0
for day in range(1, D + 1):
dk = apsp(N, es, [x != day for x in r])[iu]
num += int((dk - d0).sum())
den = D * N * (N - 1) // 2
avg = float(num) / float(den) * 1000.0
return float(rust_round(avg)), {"num": num}