-
Notifications
You must be signed in to change notification settings - Fork 27
/
222.go
58 lines (51 loc) · 1.39 KB
/
222.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
// UVa 222 - Budget Travel
package main
import (
"fmt"
"math"
"os"
)
type station struct{ distance, price float64 }
var (
num int
stations []station
destination, capacity, mpg, minCost float64
)
func backtracking(cost, location, remaining float64, curr int) {
if curr+1 == num {
if remaining-(destination-location)/mpg >= 0 && cost < minCost {
minCost = cost
}
return
}
for i := curr + 1; i < num; i++ {
newRemaining := remaining - (stations[i].distance-location)/mpg
if remaining >= 0 {
backtracking(cost, stations[i].distance, newRemaining, i)
}
if remaining < 0 || remaining >= 0 && newRemaining <= capacity/2 {
backtracking(cost+(capacity-newRemaining)*stations[i].price*0.01+2, stations[i].distance, capacity, i)
}
}
}
func main() {
in, _ := os.Open("222.in")
defer in.Close()
out, _ := os.Create("222.out")
defer out.Close()
var initCost float64
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%f", &destination); destination < 0 {
break
}
fmt.Fscanf(in, "%f%f%f%d", &capacity, &mpg, &initCost, &num)
stations = make([]station, num)
for i := range stations {
fmt.Fscanf(in, "%f%f", &stations[i].distance, &stations[i].price)
}
minCost = math.MaxFloat64
backtracking(initCost, 0, capacity, -1)
fmt.Fprintf(out, "Data Set #%d\n", kase)
fmt.Fprintf(out, "minimum cost = $%.2f\n", minCost)
}
}