Submission details
Task:Internet connection
Sender:MrAurela
Submission time:2020-09-24 16:53:00 +0300
Language:Python3 (CPython3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.07 sdetails
#7ACCEPTED0.06 sdetails
#8ACCEPTED0.07 sdetails
#9ACCEPTED0.07 sdetails
#10ACCEPTED0.07 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.04 sdetails
#13ACCEPTED0.04 sdetails
#14ACCEPTED0.04 sdetails
#15ACCEPTED0.04 sdetails
#16ACCEPTED0.04 sdetails
#17ACCEPTED0.04 sdetails
#18ACCEPTED0.10 sdetails
#19ACCEPTED0.04 sdetails
#20ACCEPTED0.04 sdetails
#21ACCEPTED0.04 sdetails
#22ACCEPTED0.04 sdetails

Code

import queue

#Read input
#n: number of nodes
#m: number of edges
n, m = [int(i) for i in input().split()]

#Read edge inputs and initialize edges
connections = {i: {} for i in range(1,n+1)}

for i in range(m):
    a,b,w = [int(i) for i in input().split()] #a->b with weigth w
    connections[a][b] = w
    if a not in connections[b]: #Always connect the other direction, with weigth 0
        connections[b][a] = 0

#Search for goal with Breadth-first-search
#Start from where left of when goal was found last time
def bfs(goal, left, predecessors, successors):

    #Continue until goal is found or there are no unvisited neighbors left
    while not left.empty() and goal not in predecessors:
        
        #Take first vertex from the "waiting line"
        current = left.get()

        #Create a successor set
        successors[current] = {}
        
        #Go trough every neighbor with positive speed
        for neighbor, speed in connections[current].items():
            if speed > 0:
                
                #If predecessors to neighbor was not yet found
                if neighbor not in predecessors:
                    predecessors[neighbor] = queue.Queue()
                    left.put(neighbor) #Add neighbor to visit-list
                    
                #Add path current->neighbor (unless start node)
                if predecessors[neighbor] is not None:
                    predecessors[neighbor].put(current)
                    successors[current][neighbor] = True

    #If goal was found, return path, and list of unvisited vertices
    #Otherwise (None, None)
    if goal in predecessors:
        return (left, predecessors, successors)
    else:
        return (None, None, None)

#Edits the graph by reversing the used amount of capacity of the edges
def reverse_routes(goal, predecessors, successors):
    
    routes = 10**9 #maximum speed
    route_from_start = [] #path from start to finish

    #Get maximum capacity that can be used troughout a path
    #Start from goal and go back a step at a time until start is reach.
    #
    current = goal
    previous = predecessors[current].get() #Get/Remove first path to current.
    route_from_start.append(current)

    while previous is not None:
        routes = min(routes, connections[previous][current])
        current = previous
        previouses = predecessors[current]
        #Get/Remove first path to current or get None
        if previouses is not None:
            previous = previouses.get()
        else:
            previous = None 
        route_from_start.append(current)

    #Reverse directions by used capacity
    current = route_from_start.pop()
    
    while len(route_from_start) > 0:
        nex = route_from_start.pop()
        connections[current][nex] -= routes
        connections[nex][current] += routes
        """
        if not path[nex].empty():
            #BALBALBALABAL
        """
        current = nex
        
    return routes


#Initialize a queue for BFS with starting vertex
left = queue.Queue()
left.put(1)

#Initialize list of predeccors and successors
predecessors = {1: None}
successors = {}

#Initialize total capacity count
routes = 0

#Continue until no new path is found
while predecessors is not None:
    #print(routes)

    #DEBUG
    left = queue.Queue()
    left.put(1)
    predecessors = {1: None}
    successors = {}

    #Call BFS
    left, predecessors, successors = bfs(n, left, predecessors, successors)

    #If path was found, add the new count and edit the graph
    if predecessors is not None:
        new_routes = reverse_routes(n, predecessors, successors)
        routes += new_routes


#Print answer:
print(routes)

Test details

Test 1

Verdict: ACCEPTED

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

correct output
73

user output
73

Test 2

Verdict: ACCEPTED

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

correct output
110

user output
110

Test 3

Verdict: ACCEPTED

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

correct output
29

user output
29

Test 4

Verdict: ACCEPTED

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

correct output
95

user output
95

Test 5

Verdict: ACCEPTED

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

correct output
156

user output
156

Test 6

Verdict: ACCEPTED

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

correct output
4397669179

user output
4397669179

Test 7

Verdict: ACCEPTED

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

correct output
5298558023

user output
5298558023

Test 8

Verdict: ACCEPTED

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

correct output
5466229311

user output
5466229311

Test 9

Verdict: ACCEPTED

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

correct output
4893925638

user output
4893925638

Test 10

Verdict: ACCEPTED

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

correct output
6956499595

user output
6956499595

Test 11

Verdict: ACCEPTED

input
2 1
1 2 1

correct output
1

user output
1

Test 12

Verdict: ACCEPTED

input
2 1
2 1 1

correct output
0

user output
0

Test 13

Verdict: ACCEPTED

input
2 2
1 2 1
2 1 1

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

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

correct output
4

user output
4

Test 16

Verdict: ACCEPTED

input
2 0

correct output
0

user output
0

Test 17

Verdict: ACCEPTED

input
100 0

correct output
0

user output
0

Test 18

Verdict: ACCEPTED

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

correct output
98000000000

user output
98000000000

Test 19

Verdict: ACCEPTED

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

correct output
1000000000

user output
1000000000

Test 20

Verdict: ACCEPTED

input
2 2
2 1 1
1 2 1

correct output
1

user output
1

Test 21

Verdict: ACCEPTED

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

correct output
2000000000

user output
2000000000

Test 22

Verdict: ACCEPTED

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

correct output
2000000000

user output
2000000000