-
Notifications
You must be signed in to change notification settings - Fork 27
/
820.go
68 lines (63 loc) · 1.44 KB
/
820.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// UVa 820 - Internet Bandwidth
package main
import (
"fmt"
"math"
"os"
)
func edmondsKarp(s, t int, matrix [][]int) int {
var sum int
n := len(matrix)
parent := make([]int, n)
flow := make([][]int, n)
for i := range flow {
flow[i] = make([]int, n)
}
for {
capacity := make([]int, n)
capacity[s] = math.MaxInt32
for queue := []int{s}; len(queue) > 0 && capacity[t] == 0; queue = queue[1:] {
curr := queue[0]
for i := 1; i < n; i++ {
if capacity[i] == 0 && matrix[curr][i] > flow[curr][i] {
queue = append(queue, i)
parent[i] = curr
capacity[i] = min(capacity[curr], matrix[curr][i]-flow[curr][i])
}
}
}
if capacity[t] == 0 {
break
}
for curr := t; curr != s; curr = parent[curr] {
flow[parent[curr]][curr] += capacity[t]
flow[curr][parent[curr]] -= capacity[t]
}
sum += capacity[t]
}
return sum
}
func main() {
in, _ := os.Open("820.in")
defer in.Close()
out, _ := os.Create("820.out")
defer out.Close()
var n, s, t, c, n1, n2, bw int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fscanf(in, "%d%d%d", &s, &t, &c)
matrix := make([][]int, n+1)
for i := range matrix {
matrix[i] = make([]int, n+1)
}
for ; c > 0; c-- {
fmt.Fscanf(in, "%d%d%d", &n1, &n2, &bw)
matrix[n1][n2] += bw
matrix[n2][n1] = matrix[n1][n2]
}
fmt.Fprintf(out, "Network %d\n", kase)
fmt.Fprintf(out, "The bandwidth is %d.\n\n", edmondsKarp(s, t, matrix))
}
}