-
Notifications
You must be signed in to change notification settings - Fork 26
/
10911.go
74 lines (66 loc) · 1.36 KB
/
10911.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
69
70
71
72
73
74
// UVa 10911 - Forming Quiz Teams
package main
import (
"fmt"
"math"
"os"
)
var (
num uint32
dp []float64
dist [][]float64
)
func distance(s1, s2 [2]int) float64 {
return math.Sqrt(float64((s1[0]-s2[0])*(s1[0]-s2[0]) + (s1[1]-s2[1])*(s1[1]-s2[1])))
}
func dfs(level int) float64 {
if dp[level] == math.MaxFloat64 {
if level == (1<<num)-1 {
dp[level] = 0
} else {
for i := uint32(0); i < num-1; i++ {
if level&(1<<i) == 0 {
for j := i + 1; j < num; j++ {
if level&(1<<j) == 0 {
dp[level] = min(dp[level], dist[i][j]+dfs(level|1<<i|1<<j))
}
}
}
}
}
}
return dp[level]
}
func solve(students [][2]int) float64 {
dist = make([][]float64, num)
for i := range dist {
dist[i] = make([]float64, num)
for j := range dist[i] {
dist[i][j] = distance(students[i], students[j])
}
}
dp = make([]float64, 1<<num)
for i := range dp {
dp[i] = math.MaxFloat64
}
return dfs(0)
}
func main() {
in, _ := os.Open("10911.in")
defer in.Close()
out, _ := os.Create("10911.out")
defer out.Close()
var n uint32
var name string
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
num = 2 * n
students := make([][2]int, num)
for i := range students {
fmt.Fscanf(in, "%s%d%d", &name, &students[i][0], &students[i][1])
}
fmt.Fprintf(out, "Case %d: %.2f\n", kase, solve(students))
}
}