-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_naive.py
62 lines (40 loc) · 1.39 KB
/
solve_naive.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
import algorithm
import sanity
import supreme_mst
import greedy
T = 495 # number of test cases
old_solutions = []
fold = open("answer.out", "r")
for _ in range(T):
old_solutions = old_solutions + [[int(x) for x in fold.readline().split()]]
fout = open ("answer.out", "w")
for t in range(1, T+1):
fin = open("./instances/" + str(t) + ".in", "r")
N = int(fin.readline())
d = [[] for i in range(N)]
for i in range(N):
d[i] = [int(x) for x in fin.readline().split()]
c = fin.readline()
# find an answer, and put into assign
print ("*** TRIAL " + str(t) + " ***")
old_sol = sanity.denormalize(old_solutions[t-1], N)
old_score = sanity.weight(old_sol,d)
if old_score == 0:
path = old_sol
else:
path = greedy.good_greedy(d, c, N)
if path is not None and sanity.is_valid_path(path, c):
post = sanity.weight(path, d)
if post < old_score:
print ("NEW: " + str(post))
print ("OLD: " + str(old_score))
print ("IMPROVEMENT: " + str(old_score-post))
print ("")
if len(old_sol) > 0 and sanity.weight(old_sol,d) < post:
path = old_sol
post = sanity.weight(old_sol,d)
else:
path = old_sol
path = sanity.normalize(path, N)
fout.write("%s\n" % " ".join(map(str, path)))
fout.close()