Skip to content

Commit 460aeed

Browse files
committedFeb 28, 2021
chapter 13 implementing
1 parent 04c381e commit 460aeed

File tree

13 files changed

+285
-0
lines changed

13 files changed

+285
-0
lines changed
 

‎chapter_13/minimum_spaning_tree/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "minimum_spaning_tree"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
-1 2 3 1 -1
3+
2 -1 -1 4 -1
4+
3 -1 -1 1 1
5+
1 4 1 -1 3
6+
-1 -1 1 3 -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#[derive(Clone, PartialEq)]
2+
enum NodeState {
3+
UnReached,
4+
Reached,
5+
Finished,
6+
}
7+
8+
fn prim(graph: &mut Vec<Vec<i32>>, node_count: usize) -> Vec<i32> {
9+
let mut nodestate: Vec<NodeState> = vec![NodeState::UnReached; node_count];
10+
let mut next_node: Vec<i32> = vec![std::i32::MAX; node_count];
11+
let mut minpath_to_node: Vec<i32> = vec![std::i32::MAX; node_count];
12+
// start from node 0
13+
next_node[0] = -1;
14+
minpath_to_node[0] =0;
15+
16+
loop {
17+
let mut minpath_to_unfinished_node = std::i32::MAX;
18+
let mut path_fnished_node: usize = 0;
19+
for node in 0.. node_count {
20+
if nodestate[node] != NodeState::Finished && minpath_to_unfinished_node > minpath_to_node[node] {
21+
path_fnished_node = node;
22+
minpath_to_unfinished_node = minpath_to_node[node];
23+
}
24+
}
25+
26+
if minpath_to_unfinished_node == std::i32::MAX {
27+
break;
28+
}
29+
30+
nodestate[path_fnished_node] = NodeState::Finished;
31+
32+
for node in 0..node_count {
33+
if nodestate[node] != NodeState::Finished && graph[path_fnished_node][node] != std::i32::MAX {
34+
if graph[path_fnished_node][node] < minpath_to_node[node] {
35+
minpath_to_node[node] = graph[path_fnished_node][node];
36+
next_node[node] = path_fnished_node as i32;
37+
nodestate[node] = NodeState::Reached;
38+
}
39+
}
40+
}
41+
}
42+
minpath_to_node
43+
}
44+
45+
fn main() {
46+
let node_counts: usize = input::read_number();
47+
let mut graph: Vec<Vec<i32>> = vec![vec![std::i32::MAX; node_counts]; node_counts];
48+
for node_index in 0..node_counts {
49+
let node_info: Vec<i32> = input::read_numline();
50+
for i in 0..node_info.len() {
51+
if node_info[i] == -1 {
52+
continue;
53+
}
54+
graph[node_index][i] = node_info[i];
55+
}
56+
}
57+
let minpath_to_node = prim(&mut graph, node_counts);
58+
let pathsum: i32 = minpath_to_node.into_iter().sum();
59+
println!("mininum_path sum is {}", pathsum);
60+
}

‎chapter_13/shortest_path/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_13/shortest_path/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "shortest_path"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_13/shortest_path/input.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
0 3 2 3 3 1 1 2
3+
1 2 0 2 3 4
4+
2 3 0 3 3 1 4 1
5+
3 4 2 1 0 1 1 4 4 3
6+
4 2 2 1 3 3

‎chapter_13/shortest_path/src/main.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#[derive(Clone, PartialEq)]
2+
enum NodeState {
3+
UnReached,
4+
Reached,
5+
Finished,
6+
}
7+
8+
fn dikkstra(node_count: usize, graph: &mut Vec<Vec<i32>>) -> Vec<i32> {
9+
let mut cost_from_start: Vec<i32> = vec![std::i32::MAX; node_count];
10+
let mut node_state: Vec<NodeState> = vec![NodeState::UnReached; node_count];
11+
// start form 0
12+
cost_from_start[0] = 0;
13+
14+
loop {
15+
let mut is_unfinished_node_fount = false;
16+
let mut cost_of_this_node = std::i32::MAX;
17+
let mut cost_calc_finished_node: usize = 0;
18+
19+
for node in 0..node_count {
20+
if node_state[node] != NodeState::Finished && cost_from_start[node] != std::i32::MAX {
21+
if cost_from_start[node] < cost_of_this_node{
22+
cost_of_this_node = cost_from_start[node];
23+
is_unfinished_node_fount = true;
24+
cost_calc_finished_node = node;
25+
node_state[node] = NodeState::Reached;
26+
}
27+
}
28+
}
29+
30+
if !is_unfinished_node_fount { break; }
31+
node_state[cost_calc_finished_node] = NodeState::Finished;
32+
33+
for node in 0..node_count {
34+
if node_state[node] != NodeState::Finished && graph[cost_calc_finished_node][node] != std::i32::MAX {
35+
if (cost_from_start[cost_calc_finished_node] + graph[cost_calc_finished_node][node]) < cost_from_start[node] {
36+
cost_from_start[node] = cost_from_start[cost_calc_finished_node] + graph[cost_calc_finished_node][node];
37+
node_state[node] = NodeState::Reached;
38+
}
39+
}
40+
}
41+
}
42+
cost_from_start
43+
}
44+
45+
fn main() {
46+
let node_count: usize = input::read_number();
47+
let mut graph: Vec<Vec<i32>> = vec![vec![std::i32::MAX; node_count]; node_count];
48+
for _ in 0..node_count {
49+
let node_info: Vec<usize> = input::read_numline();
50+
let node = node_info[0];
51+
let node_branch_count = node_info[1];
52+
for i in 0..node_branch_count {
53+
let node_to = node_info[2+i*2];
54+
let path_cost = node_info[2+i*2+1] as i32;
55+
graph[node][node_to] = path_cost;
56+
}
57+
}
58+
let cost_from_start = dikkstra(node_count, &mut graph);
59+
for (node, cost) in cost_from_start.iter().enumerate() {
60+
println!("{} {}", node, cost);
61+
}
62+
}

‎chapter_13/shortest_path_2/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_13/shortest_path_2/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "shortest_path_2"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_13/shortest_path_2/input.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
0 3 2 3 3 1 1 2
3+
1 2 0 2 3 4
4+
2 3 0 3 3 1 4 1
5+
3 4 2 1 0 1 1 4 4 3
6+
4 2 2 1 3 3
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
use std::collections::BinaryHeap;
3+
use std::i32;
4+
use std::cmp::Ordering;
5+
6+
#[derive(Clone, PartialEq)]
7+
enum NodeState {
8+
UnReached,
9+
Reached,
10+
Finished,
11+
}
12+
13+
#[derive(Copy, Clone, Eq, PartialEq)]
14+
struct State {
15+
cost: usize,
16+
node: usize,
17+
}
18+
19+
impl Ord for State {
20+
fn cmp(&self, other: &Self) -> Ordering {
21+
// maxヒープをminヒープとして使用するためには,
22+
// コストがより小さい方がGreaterとなるよう順序を反転する
23+
// つまり自身よりも大きいコストと比較したら, 自身がより大きい(MAXヒープで上)という判断にする
24+
// ノード番号は若い順でよい
25+
other.cost.cmp(&self.cost)
26+
.then_with(|| self.node.cmp(&other.node))
27+
}
28+
}
29+
impl PartialOrd for State {
30+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31+
Some(self.cmp(other))
32+
}
33+
}
34+
35+
/*
36+
37+
fn dikkstra(node_count: usize, graph: &mut Vec<Vec<(usize, i32)>>) {
38+
let mut node_state: Vec<NodeState> = vec![NodeState::UnReached; node_count];
39+
let mut cost_from_start :BinaryHeap<(usize, i32)> = BinaryHeap::new();
40+
41+
// start from node 0
42+
cost_from_start[0] = 0;
43+
44+
loop {
45+
let mut cost_to_node = i32::MAX;
46+
let mut is_unfinished_node_found = false;
47+
48+
for
49+
50+
}
51+
}
52+
*/
53+
fn main() {
54+
55+
let node_count: usize = input::read_number();
56+
let mut graph: Vec<Vec<(usize, i32)>> = vec![vec![]; node_count];
57+
for _ in 0..node_count {
58+
let node_info: Vec<usize> = input::read_numline();
59+
let node = node_info[0];
60+
let node_branch_count = node_info[1];
61+
for i in 0..node_branch_count {
62+
let node_to = node_info[2+i*2];
63+
let path_cost = node_info[2+i*2+1] as i32;
64+
graph[node].push((node_to, path_cost));
65+
}
66+
}
67+
/*
68+
let cost_from_start = dikkstra(node_count, &mut graph);
69+
for (node, cost) in cost_from_start.iter().enumerate() {
70+
println!("{} {}", node, cost);
71+
}
72+
*/
73+
74+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4 5
2+
4 2
3+
5 2
4+
2 1
5+
8 3

0 commit comments

Comments
 (0)