|
@@ -0,0 +1,68 @@
|
|
1
|
+import numpy as np
|
|
2
|
+
|
|
3
|
+def shortest(i, j, ABG):
|
|
4
|
+ minis = {}
|
|
5
|
+ for A, B, G in ABG:
|
|
6
|
+ if A != i and B != i:
|
|
7
|
+ continue
|
|
8
|
+ if A == i:
|
|
9
|
+ x = B
|
|
10
|
+ else:
|
|
11
|
+ x = A
|
|
12
|
+ if x not in minis or minis[x] > G:
|
|
13
|
+ minis[x] = G
|
|
14
|
+ direct = -1
|
|
15
|
+ if j in minis:
|
|
16
|
+ direct = minis[j]
|
|
17
|
+ for x, dist in enumerate(minis):
|
|
18
|
+ if direct > dist:
|
|
19
|
+ distToJ = shortest(x, j, ABG)
|
|
20
|
+ if distToJ < ...
|
|
21
|
+
|
|
22
|
+def routes(ABG, SD):
|
|
23
|
+ r = {}
|
|
24
|
+ townsInSD = set()
|
|
25
|
+ townsInABG = set()
|
|
26
|
+ # We only compute the routes for towns in SD
|
|
27
|
+ for S, D in SD:
|
|
28
|
+ # print("S={0}, D={1}".format(S, D))
|
|
29
|
+ townsInSD.add(S)
|
|
30
|
+ townsInSD.add(D)
|
|
31
|
+ for A, B, G in ABG:
|
|
32
|
+ # print("A={0}, B={1}, G={2}".format(A, B, G))
|
|
33
|
+ townsInABG.add(A)
|
|
34
|
+ townsInABG.add(B)
|
|
35
|
+ print(townsInSD)
|
|
36
|
+ print(townsInABG)
|
|
37
|
+ if len(townsInABG) < len(townsInSD):
|
|
38
|
+ return {}
|
|
39
|
+ allTowns = townsInSD | townsInABG
|
|
40
|
+ for i in townsInSD:
|
|
41
|
+ for j in townsInSD:
|
|
42
|
+ if (i,j) in r:
|
|
43
|
+ continue
|
|
44
|
+ dist = shortest(i, j, ABG)
|
|
45
|
+ r[(i,j)] = dist
|
|
46
|
+ r[(j,i)] = dist
|
|
47
|
+ return r
|
|
48
|
+
|
|
49
|
+def solve(N, M, K, ABG, SD):
|
|
50
|
+ print("N={0}, M={1}, K={2}, ABG={3}, SD={4}".format(N, M, K, ABG, SD))
|
|
51
|
+ if K <= 0:
|
|
52
|
+ return 0
|
|
53
|
+ r = routes(ABG, SD)
|
|
54
|
+ if r == {}:
|
|
55
|
+ return -1
|
|
56
|
+ return 'gnagna'
|
|
57
|
+
|
|
58
|
+if __name__ == "__main__":
|
|
59
|
+ import fileinput
|
|
60
|
+ f = fileinput.input()
|
|
61
|
+
|
|
62
|
+ T = int(f.readline())
|
|
63
|
+ for case in range(1, T+1):
|
|
64
|
+ N, M, K = [int(i) for i in f.readline().split()]
|
|
65
|
+ ABG = [[int(i) for i in f.readline().split()] for x in range(M)]
|
|
66
|
+ SD = [[int(i) for i in f.readline().split()] for x in range(K)]
|
|
67
|
+ solution = solve(N, M, K, ABG, SD)
|
|
68
|
+ print("Case #{0}: {1}".format(case, solution))
|