浏览代码

Round 1 : Manic Moving - partial

Gildas Chabot 8 年之前
父节点
当前提交
0013da2199
共有 2 个文件被更改,包括 116 次插入0 次删除
  1. 68 0
      2017.1-RoundOne/c.ManiacMoving.py
  2. 48 0
      2017.1-RoundOne/manic_moving_example_input.txt

+ 68 - 0
2017.1-RoundOne/c.ManiacMoving.py

@@ -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))

+ 48 - 0
2017.1-RoundOne/manic_moving_example_input.txt

@@ -0,0 +1,48 @@
1
+5
2
+3 2 3
3
+1 2 4
4
+2 3 7
5
+2 1
6
+3 2
7
+3 2
8
+3 2 3
9
+1 2 4
10
+2 3 7
11
+3 2
12
+2 1
13
+3 2
14
+4 4 4
15
+1 2 3
16
+1 3 1
17
+2 4 10
18
+3 4 1
19
+1 2
20
+2 4
21
+4 1
22
+3 1
23
+4 2 4
24
+1 2 3
25
+1 3 1
26
+1 2
27
+2 4
28
+4 1
29
+3 1
30
+7 8 10
31
+7 5 9
32
+1 2 14
33
+3 7 7
34
+2 5 8
35
+4 3 10
36
+1 4 3
37
+7 3 5
38
+4 2 16
39
+3 2
40
+5 1
41
+7 3
42
+5 3
43
+1 7
44
+3 2
45
+1 5
46
+4 7
47
+7 2
48
+5 1