Skip to content

Commit ec9fb5a

Browse files
committed
[2300] CF1314D 随机化 二分图染色 DP
1 parent 5fe35dc commit ec9fb5a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

main/1300-1399/1314D.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
"math/rand"
8+
"time"
9+
)
10+
11+
// github.com/EndlessCheng/codeforces-go
12+
func CF1314D(_r io.Reader, out io.Writer) {
13+
rand.Seed(time.Now().UnixNano())
14+
in := bufio.NewReader(_r)
15+
min := func(a, b int) int {
16+
if a > b {
17+
return b
18+
}
19+
return a
20+
}
21+
22+
var n, k int
23+
Fscan(in, &n, &k)
24+
dis := make([][]int, n)
25+
for i := range dis {
26+
dis[i] = make([]int, n)
27+
for j := range dis[i] {
28+
Fscan(in, &dis[i][j])
29+
}
30+
}
31+
32+
ans := int(1e9)
33+
dp := make([]int, n)
34+
color := make([]int, n)
35+
for t := 5000; t > 0; t-- {
36+
dp[0] = 0
37+
for i := 1; i < n; i++ {
38+
dp[i] = 1e9
39+
color[i] = rand.Intn(2)
40+
}
41+
for i := 0; i < k; i++ {
42+
for v, c := range color {
43+
if c != i&1 {
44+
dp[v] = 1e9
45+
for from, c := range color {
46+
if c == i&1 {
47+
// dp[i][v] 表示走了 i 条边,当前在节点 v 时的最小花费,那么答案就是 dp[k][0]
48+
// 其中第一维可以压缩掉
49+
dp[v] = min(dp[v], dp[from]+dis[from][v])
50+
}
51+
}
52+
}
53+
}
54+
}
55+
ans = min(ans, dp[0])
56+
}
57+
Fprint(out, ans)
58+
}
59+
60+
//func main() { CF1314D(os.Stdin, os.Stdout) }

main/1300-1399/1314D_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// CF1310D
9+
10+
// https://codeforces.com/problemset/problem/1314/D
11+
// https://codeforces.com/problemset/status/1314/problem/D
12+
func TestCF1314D(t *testing.T) {
13+
// just copy from website
14+
rawText := `
15+
inputCopy
16+
5 8
17+
0 1 2 2 0
18+
0 0 1 1 2
19+
0 1 0 0 0
20+
2 1 1 0 0
21+
2 0 1 2 0
22+
outputCopy
23+
2
24+
inputCopy
25+
3 2
26+
0 1 1
27+
2 0 1
28+
2 2 0
29+
outputCopy
30+
3`
31+
testutil.AssertEqualCase(t, rawText, 0, CF1314D)
32+
}

0 commit comments

Comments
 (0)