-
Notifications
You must be signed in to change notification settings - Fork 3
/
checker.py
72 lines (61 loc) · 2.26 KB
/
checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from dmoj.result import CheckerResult
from dmoj.executors import executors
import subprocess
from collections import defaultdict
from re import split as resplit
import math
def getNorm(n, y, L):
ret = 0.0
for i in range(n):
for j in L[i]:
ret += L[i][j] * ((y[j] - y[i]) ** 2)
return ret ** 0.5
def getL(judge_input):
# from the raw input
n = int(judge_input[1].split()[0])
m = int(judge_input[1].split()[2])
wMat = []
for _ in range(n):
wMat.append(dict())
for i in range(2, m+2):
data = judge_input[i].split()
u = int(data[0]) - 1
v = int(data[1]) - 1
w = float(data[2])
wMat[u][v] = w
wMat[v][u] = w
return wMat
def check(process_output, judge_output, judge_input, point_value, execution_time, **kwargs):
try:
process_lines = process_output.decode("utf-8").split("\n")[:-1]
judge_lines = judge_output.decode("utf-8").split("\n")[1:-1]
judge_input = judge_input.decode("utf-8").split("\n")
id = judge_input[0]
n = int(judge_input[1].split()[0])
assert n == len(process_lines), "wrong number of lines {}".format(len(process_lines))
barx = [float(x) for x in judge_lines]
try:
x = [float(z) for z in process_lines]
except:
assert False, "illegal output"
for y in x:
assert not math.isnan(y), "nan detected"
assert len(barx) == n, "barx has wrong length {}".format(len(barx))
assert len(x) == n, "x has wrong length {}".format(len(x))
L = getL(judge_input)
xDiff = [x[i] - barx[i] for i in range(n)]
xDiffNorm = getNorm(n, xDiff, L)
xNorm = getNorm(n, barx, L)
if xDiffNorm > 0.1 * xNorm:
return CheckerResult(
False,
0,
"xNorm = {0:.2f}, diff = {1:.2f}, ratio = {2:.2f}".format(xNorm, xDiffNorm, xDiffNorm / xNorm))
bestTime = 10.
ratio = bestTime / execution_time
return CheckerResult(
True,
point_value * ratio**2,
"xNorm = {0:.2f}, diff = {1:.2f}, ratio = {2:.2f}".format(xNorm, xDiffNorm, xDiffNorm / xNorm))
except Exception as e:
return CheckerResult(False, 0, "{}".format(e))