|
@@ -0,0 +1,48 @@
|
|
1
|
+import numpy as np
|
|
2
|
+
|
|
3
|
+def calcCost(sol, S):
|
|
4
|
+ res = 0
|
|
5
|
+ for i, n in enumerate(sol):
|
|
6
|
+ for j in range(n):
|
|
7
|
+ # print('res+=', S[i][j])
|
|
8
|
+ res += S[i][j]
|
|
9
|
+ if n > 0:
|
|
10
|
+ # print('resb+=', pow(2, n), n)
|
|
11
|
+ res += pow(n, 2)
|
|
12
|
+ return res
|
|
13
|
+
|
|
14
|
+def solveRec(N, M, S):
|
|
15
|
+ if N == 0:
|
|
16
|
+ return []
|
|
17
|
+ else:
|
|
18
|
+ sol = solveRec(N-1, M, S)
|
|
19
|
+ sol.append(0)
|
|
20
|
+ minCost = -1
|
|
21
|
+ for i in range(N):
|
|
22
|
+ if sol[i] >= len(S[i]):
|
|
23
|
+ continue
|
|
24
|
+ newSol = list(sol)
|
|
25
|
+ newSol[i] += 1
|
|
26
|
+ newCost = calcCost(newSol, S)
|
|
27
|
+ if newCost < minCost or minCost == -1:
|
|
28
|
+ minCost = newCost
|
|
29
|
+ bestSol = newSol
|
|
30
|
+ return bestSol
|
|
31
|
+
|
|
32
|
+def solve(N, M, S):
|
|
33
|
+ # print("N={0}, M={1}".format(N, M))
|
|
34
|
+ # print(S)
|
|
35
|
+ sol = solveRec(N,M,S)
|
|
36
|
+ # print(sol)
|
|
37
|
+ return calcCost(sol, S)
|
|
38
|
+
|
|
39
|
+if __name__ == "__main__":
|
|
40
|
+ import fileinput
|
|
41
|
+ f = fileinput.input()
|
|
42
|
+
|
|
43
|
+ T = int(f.readline())
|
|
44
|
+ for case in range(1, T+1):
|
|
45
|
+ N, M = [int(i) for i in f.readline().split()]
|
|
46
|
+ S = [sorted([int(i) for i in f.readline().split()]) for x in range(N)]
|
|
47
|
+ solution = solve(N, M, S)
|
|
48
|
+ print("Case #{0}: {1}".format(case, solution))
|