-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10330.go
More file actions
79 lines (74 loc) · 1.67 KB
/
10330.go
File metadata and controls
79 lines (74 loc) · 1.67 KB
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
69
70
71
72
73
74
75
76
77
78
79
// UVa 10330 - Power Transmission
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("10330.in")
defer in.Close()
out, _ := os.Create("10330.out")
defer out.Close()
var num, capacity, m, n1, n2, b, d int
for {
if _, err := fmt.Fscanf(in, "%d", &num); err != nil {
break
}
n := 2*num + 1
matrix := make([][]int, n+1)
for i := range matrix {
matrix[i] = make([]int, n+1)
}
for i := 1; i <= num; i++ {
fmt.Fscanf(in, "%d", &capacity)
matrix[i][i+num] = capacity
}
for fmt.Fscanf(in, "%d", &m); m > 0; m-- {
fmt.Fscanf(in, "%d%d%d", &n1, &n2, &capacity)
matrix[n1+num][n2] = capacity
}
fmt.Fscanf(in, "%d%d", &b, &d)
for ; b > 0; b-- {
fmt.Fscanf(in, "%d", &n1)
matrix[0][n1] = math.MaxInt32
}
for ; d > 0; d-- {
fmt.Fscanf(in, "%d", &n1)
matrix[n1+num][n] = math.MaxInt32
}
fmt.Fprintln(out, edmondsKarp(0, n, matrix))
}
}