Submission details
Task:Internet connection
Sender:m-x-doctor
Submission time:2020-09-19 14:20:27 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.05 sdetails
#20.05 sdetails
#30.05 sdetails
#40.05 sdetails
#50.05 sdetails
#60.23 sdetails
#70.40 sdetails
#80.16 sdetails
#90.15 sdetails
#100.34 sdetails
#110.05 sdetails
#120.05 sdetails
#130.05 sdetails
#140.18 sdetails
#150.05 sdetails
#160.05 sdetails
#170.05 sdetails
#180.17 sdetails
#190.06 sdetails
#200.05 sdetails
#210.05 sdetails
#220.05 sdetails

Code

"""https://cses.fi/345/task/B"""
from __future__ import print_function
import sys

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

def get_path_with_dfs(adj_matrix, V):
    """DFS uses a stack/list. BFS uses a queue."""
    root = 1  # Teemu
    target = V  # Taina
    seen = []  # Stuff you've seen.
    to_visit = [root]
    path = []
    while to_visit:
        v = to_visit.pop()  # Pop last inserted element.
        seen.append(v)
        found = False
        for neighbor in range(1, V+1):
            weight = adj_matrix[v][neighbor]
            if (weight < 1) or (neighbor in seen) or (neighbor in path):
                continue
            eprint(f"weight from {v} to {neighbor} is {weight}")
            found = True
            if neighbor == target:
                path.append(v)
                path.append(target)
                return path
            to_visit.append(neighbor)
            eprint('to visit', to_visit)
        if found:
            path.append(v)
            eprint("FOUND: path is ", path)
    return None  # No path found


def max_rate(adj_matrix, V):
    total_max_rate = 0
    path = get_path_with_dfs(adj_matrix, V)
    while path:
        eprint("path", path)
        path_weights = []
        for i in range(len(path)-1):
            a, b = path[i], path[i+1]
            assert(adj_matrix[a][b] == adj_matrix[b][a])
            weight = adj_matrix[a][b]
            path_weights.append(weight)
        eprint('weights', path_weights)

        max_rate = min(path_weights)
        total_max_rate += max_rate
        # Deduct the max_rate from all weights
        for i in range(len(path)-1):
            a, b = path[i], path[i+1]
            adj_matrix[a][b] -= max_rate
            adj_matrix[b][a] -= max_rate
        path = get_path_with_dfs(adj_matrix, V)
    eprint(int(total_max_rate))
    return


# Read number oof vertices (computers) and edges (connections).
V, E = [int(x) for x in input().split()]
adj_matrix = [[0 for i in range(V + 1)] for i in range(V + 1)]
for i in range(E):
    a, b, weight = [int(x) for x in input().split()]
    adj_matrix[a][b] = weight
    adj_matrix[b][a] = weight
max_rate(adj_matrix, V)

Test details

Test 1

Verdict:

input
10 20
5 6 19
4 5 47
3 5 7
4 9 62
...

correct output
73

user output
(empty)

Error:
weight from 1 to 2 is 78
to visit [2]
weight from 1 to 4 is 14
to visit [2, 4]
weight from...

Test 2

Verdict:

input
10 20
2 4 63
7 9 54
6 7 16
2 3 9
...

correct output
110

user output
(empty)

Error:
weight from 1 to 2 is 88
to visit [2]
weight from 1 to 10 is 22
path [1, 10]
weights [22]...

Test 3

Verdict:

input
10 20
5 6 90
2 3 46
7 8 80
6 7 60
...

correct output
29

user output
(empty)

Error:
weight from 1 to 2 is 26
to visit [2]
weight from 1 to 6 is 3
to visit [2, 6]
FOUND: path...

Test 4

Verdict:

input
10 20
3 4 76
5 7 8
3 8 71
4 7 24
...

correct output
95

user output
(empty)

Error:
weight from 1 to 2 is 89
to visit [2]
weight from 1 to 8 is 80
to visit [2, 8]
FOUND: path...

Test 5

Verdict:

input
10 20
1 8 22
6 7 40
4 5 20
8 10 77
...

correct output
156

user output
(empty)

Error:
weight from 1 to 2 is 53
to visit [2]
weight from 1 to 8 is 22
to visit [2, 8]
weight from...

Test 6

Verdict:

input
100 1000
63 85 864540192
22 91 974117435
64 66 953124912
85 88 6080960
...

correct output
4397669179

user output
(empty)

Error:
weight from 1 to 2 is 636542018
to visit [2]
weight from 1 to 9 is 517915875
to visit [2,...

Test 7

Verdict:

input
100 1000
36 93 760720873
12 75 175717522
78 79 340128710
80 83 181753465
...

correct output
5298558023

user output
(empty)

Error:
weight from 1 to 2 is 725914394
to visit [2]
weight from 1 to 9 is 317534926
to visit [2,...

Test 8

Verdict:

input
100 1000
20 60 909693891
55 91 570199535
21 41 118646902
37 82 824735480
...

correct output
5466229311

user output
(empty)

Error:
weight from 1 to 2 is 387326445
to visit [2]
weight from 1 to 7 is 862213124
to visit [2,...

Test 9

Verdict:

input
100 1000
26 44 753330451
62 67 821574279
70 95 219303983
7 44 980013084
...

correct output
4893925638

user output
(empty)

Error:
weight from 1 to 2 is 963511849
to visit [2]
weight from 1 to 4 is 219769993
to visit [2,...

Test 10

Verdict:

input
100 1000
15 89 501388091
50 71 396801720
15 92 324349822
29 85 184420157
...

correct output
6956499595

user output
(empty)

Error:
weight from 1 to 2 is 434654854
to visit [2]
weight from 1 to 4 is 663665104
to visit [2,...

Test 11

Verdict:

input
2 1
1 2 1

correct output
1

user output
(empty)

Error:
weight from 1 to 2 is 1
path [1, 2]
weights [1]
1

Test 12

Verdict:

input
2 1
2 1 1

correct output
0

user output
(empty)

Error:
weight from 1 to 2 is 1
path [1, 2]
weights [1]
1

Test 13

Verdict:

input
2 2
1 2 1
2 1 1

correct output
1

user output
(empty)

Error:
weight from 1 to 2 is 1
path [1, 2]
weights [1]
1

Test 14

Verdict:

input
100 1000
1 2 539540023
2 3 244306651
3 4 253259012
3 5 630461598
...

correct output
0

user output
(empty)

Error:
weight from 1 to 2 is 539540023
to visit [2]
weight from 1 to 9 is 962769780
to visit [2,...

Test 15

Verdict:

input
4 5
1 2 2
1 3 5
2 4 3
3 2 2
...

correct output
4

user output
(empty)

Error:
weight from 1 to 2 is 2
to visit [2]
weight from 1 to 3 is 5
to visit [2, 3]
FOUND: path i...

Test 16

Verdict:

input
2 0

correct output
0

user output
(empty)

Error:
0

Test 17

Verdict:

input
100 0

correct output
0

user output
(empty)

Error:
0

Test 18

Verdict:

input
100 196
1 2 1000000000
2 100 1000000000
1 3 1000000000
3 100 1000000000
...

correct output
98000000000

user output
(empty)

Error:
weight from 1 to 2 is 1000000000
to visit [2]
weight from 1 to 3 is 1000000000
to visit [2...

Test 19

Verdict:

input
100 99
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
...

correct output
1000000000

user output
(empty)

Error:
weight from 1 to 2 is 1000000000
to visit [2]
FOUND: path is  [1]
weight from 2 to 3 is 10...

Test 20

Verdict:

input
2 2
2 1 1
1 2 1

correct output
1

user output
(empty)

Error:
weight from 1 to 2 is 1
path [1, 2]
weights [1]
1

Test 21

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 3 1
2 4 1000000000
...

correct output
2000000000

user output
(empty)

Error:
weight from 1 to 2 is 1000000000
to visit [2]
weight from 1 to 3 is 1000000000
to visit [2...

Test 22

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 4 1000000000
2 3 1
...

correct output
2000000000

user output
(empty)

Error:
weight from 1 to 2 is 1000000000
to visit [2]
weight from 1 to 3 is 1000000000
to visit [2...