Task: | Internet connection |
Sender: | MrAurela |
Submission time: | 2020-09-24 20:50:34 +0300 |
Language: | Python3 (CPython3) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.04 s | details |
#2 | ACCEPTED | 0.04 s | details |
#3 | ACCEPTED | 0.04 s | details |
#4 | ACCEPTED | 0.04 s | details |
#5 | ACCEPTED | 0.04 s | details |
#6 | ACCEPTED | 0.05 s | details |
#7 | ACCEPTED | 0.04 s | details |
#8 | ACCEPTED | 0.04 s | details |
#9 | ACCEPTED | 0.04 s | details |
#10 | ACCEPTED | 0.05 s | details |
#11 | ACCEPTED | 0.04 s | details |
#12 | ACCEPTED | 0.04 s | details |
#13 | ACCEPTED | 0.04 s | details |
#14 | ACCEPTED | 0.04 s | details |
#15 | ACCEPTED | 0.04 s | details |
#16 | ACCEPTED | 0.04 s | details |
#17 | ACCEPTED | 0.04 s | details |
#18 | ACCEPTED | 0.05 s | details |
#19 | ACCEPTED | 0.04 s | details |
#20 | ACCEPTED | 0.04 s | details |
#21 | ACCEPTED | 0.04 s | details |
#22 | ACCEPTED | 0.04 s | details |
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(start, goal): #Initialize a queue for BFS with starting vertex left = queue.Queue() left.put(start) #Initialize list of predeccors predecessors = {start: None} #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() #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: left.put(neighbor) #Add neighbor to visit-list predecessors[neighbor] = current #If goal was found, return path return predecessors if goal in predecessors else None #Edits the graph by reversing the used amount of capacity of the edges def reverse_routes(goal, predecessors): routes = 10**9 #maximum speed #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] while previous is not None: routes = min(routes, connections[previous][current]) current = previous previous = predecessors[current] #Revert flows current = goal previous = predecessors[current] while previous is not None: connections[previous][current] -= routes connections[current][previous] += routes current = previous previous = predecessors[current] return routes #Count the maximum flow from start to goal def max_flow(start, goal): #Initialize total capacity count routes = 0 #Continue until no new path is found while True: #Call BFS predecessors = bfs(start, goal) #If path was found, add the new count and edit the graph #Otherwise quit if predecessors is not None: new_routes = reverse_routes(goal, predecessors) routes += new_routes else: break return routes #Print answer: print(max_flow(1,n))
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 |