-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path301.go
61 lines (54 loc) · 1.31 KB
/
301.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
// UVa 301 - Transportation
package main
import (
"fmt"
"os"
)
var (
max, capacity, orderNumber int
orders []order
capacities []int
)
type order struct{ starting, destination, passengers int }
func backtracking(level, earning int) {
if level == orderNumber {
if earning > max {
max = earning
}
return
}
backtracking(level+1, earning)
order := orders[level]
for j := order.starting; j < order.destination; j++ {
if capacities[j]+order.passengers > capacity {
return
}
}
for j := order.starting; j < order.destination; j++ {
capacities[j] += order.passengers
}
backtracking(level+1, earning+(order.destination-order.starting)*order.passengers)
for j := order.starting; j < order.destination; j++ {
capacities[j] -= order.passengers
}
}
func main() {
in, _ := os.Open("301.in")
defer in.Close()
out, _ := os.Create("301.out")
defer out.Close()
var stations int
for {
if fmt.Fscanf(in, "%d%d%d", &capacity, &stations, &orderNumber); capacity == 0 && stations == 0 && orderNumber == 0 {
break
}
orders = make([]order, orderNumber)
for i := range orders {
fmt.Fscanf(in, "%d%d%d", &orders[i].starting, &orders[i].destination, &orders[i].passengers)
}
max = 0
capacities = make([]int, stations+1)
backtracking(0, 0)
fmt.Fprintln(out, max)
}
}