-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcsa.rs
105 lines (84 loc) · 3.99 KB
/
csa.rs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::io;
use std::io::{BufReader,BufRead};
const MAX_STATIONS: usize = 100000;
#[derive(Debug)]
struct Connection {
departure_station: usize,
arrival_station: usize,
departure_timestamp: u32,
arrival_timestamp: u32
}
impl Connection {
fn parse(line: &str) -> Connection {
let mut splitted = line.split(" ").map(|crumb| { crumb.parse::<u32>().unwrap() });
Connection {
departure_station: splitted.next().unwrap() as usize,
arrival_station: splitted.next().unwrap() as usize,
departure_timestamp: splitted.next().unwrap(),
arrival_timestamp: splitted.next().unwrap(),
}
}
}
fn csa_main_loop(timetable: &[Connection], arrival_station: usize, earliest_arrival: &mut [u32], in_connection: &mut [usize]) {
let mut earliest = std::u32::MAX;
for (i, connection) in timetable.iter().enumerate() {
if connection.departure_timestamp >= earliest_arrival[connection.departure_station] &&
connection.arrival_timestamp < earliest_arrival[connection.arrival_station] {
earliest_arrival[connection.arrival_station] = connection.arrival_timestamp;
in_connection[connection.arrival_station] = i;
if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest {
earliest = connection.arrival_timestamp;
}
} else if connection.arrival_timestamp > earliest {
break;
}
}
}
fn csa_print_result(timetable: &Vec<Connection>, in_connection: &[usize], arrival_station: usize) {
if in_connection[arrival_station] == std::u32::MAX as usize {
println!("NO_SOLUTION");
} else {
let mut route = Vec::new();
let mut last_connection_index = in_connection[arrival_station];
while last_connection_index != std::u32::MAX as usize {
let ref connection = timetable[last_connection_index];
route.push(connection);
last_connection_index = in_connection[connection.departure_station];
}
for connection in route.iter().rev() {
println!("{} {} {} {}", connection.departure_station, connection.arrival_station, connection.departure_timestamp, connection.arrival_timestamp);
}
}
println!("");
}
fn csa_compute(timetable: &Vec<Connection>, departure_station: usize, arrival_station: usize, departure_time: u32)
{
let mut in_connection = vec!(std::u32::MAX as usize; MAX_STATIONS);
let mut earliest_arrival = vec!(std::u32::MAX; MAX_STATIONS);
earliest_arrival[departure_station as usize] = departure_time;
if departure_station < MAX_STATIONS && arrival_station < MAX_STATIONS {
csa_main_loop(&timetable, arrival_station, &mut earliest_arrival, &mut in_connection);
}
csa_print_result(&timetable, &in_connection, arrival_station);
}
fn main() {
// Importing connections
let mut buffered_in = BufReader::new(io::stdin()).lines();
let timetable = buffered_in.map(|r| { r.ok().expect("failed to read connection line") })
.take_while(|l| { !l.is_empty() })
.map(|l| { Connection::parse(l.trim_right()) })
.collect();
// Responding to requests from stdin
buffered_in = BufReader::new(io::stdin()).lines();
buffered_in.map(|r| { r.ok().expect("failed to read connection line") })
.take_while(|l| { !l.is_empty() })
.map(|input_line| {
let params = input_line.split(" ")
.map(|crumb| { crumb.parse().ok().expect(&format!("failed to read {} as integer", crumb)) })
.collect::<Vec<u32>>();
let departure_station = params[0] as usize;
let arrival_station = params[1] as usize;
let departure_time = params[2];
csa_compute(&timetable, departure_station, arrival_station, departure_time);
}).collect::<Vec<_>>();
}