Skip to content

Commit a4dde4d

Browse files
committed
Bellman
1 parent bf5efee commit a4dde4d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Bellman.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,59 @@ import (
44
"fmt"
55
)
66

7+
type Edge struct {
8+
u, v, weight int
9+
}
10+
11+
var edge [10]Edge
12+
var dist [10]int
13+
var maxValue int
14+
15+
var source int
16+
var nodeNum int
17+
var edgeNum int
18+
19+
func init() {
20+
maxValue = 100
21+
}
22+
23+
func initEdge() {
24+
fmt.Println("input nodeNum edgeNum source")
25+
fmt.Scanf("%d %d %d", &nodeNum, &edgeNum, &source)
26+
fmt.Println(nodeNum, edgeNum, source)
27+
28+
for i := 0; i <= nodeNum; i++ {
29+
dist[i] = maxValue
30+
}
31+
dist[source] = 0
32+
33+
for i := 0; i < edgeNum; i++ {
34+
fmt.Println("input edge.strat edge.end edge.weight")
35+
fmt.Scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].weight)
36+
if edge[i].u == source {
37+
dist[edge[i].v] = edge[i].weight
38+
}
39+
}
40+
fmt.Println(edge)
41+
fmt.Println(dist)
42+
}
43+
44+
func Bellman() {
45+
for i := 0; i < nodeNum-1; i++ {
46+
for j := 0; j < edgeNum; j++ {
47+
//开始节点 权值 >
48+
if dist[edge[j].v] > dist[edge[j].u]+edge[j].weight {
49+
dist[edge[j].v] = dist[edge[j].u] + edge[j].weight
50+
}
51+
fmt.Println(dist)
52+
}
53+
}
54+
55+
}
56+
757
func main() {
58+
//Bellman 差分约束系统 线性规划
859
fmt.Println("Bellman")
60+
initEdge()
61+
Bellman()
962
}

0 commit comments

Comments
 (0)