-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcsa.go
107 lines (87 loc) · 2.54 KB
/
csa.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
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
106
107
// gofmt -w=true *.go && go build -o csa-go csa.go && ruby test.rb ./csa-go
package main
import (
"bufio"
"fmt"
"math"
"os"
)
const maxStations = 100000
const infinity = math.MaxUint32
type Connection struct {
departureStation uint32
arrivalStation uint32
departureTime uint32
arrivalTime uint32
}
type Timetable []Connection
func readTimetable(scanner *bufio.Scanner) Timetable {
timetable := make([]Connection, 0)
for scanner.Scan() {
if len(scanner.Text()) == 0 {
break
} else {
connection := Connection{}
_, err := fmt.Sscanln(
scanner.Text(),
&connection.departureStation,
&connection.arrivalStation,
&connection.departureTime,
&connection.arrivalTime)
if err != nil {
panic(err)
}
timetable = append(timetable, connection)
}
}
return timetable
}
func (timetable Timetable) compute(departureStation uint32, arrivalStation uint32, departureTime uint32) {
// initialization
inConnection := make([]uint32, maxStations, maxStations)
earliestArrival := make([]uint32, maxStations, maxStations)
for i := 0; i < maxStations; i++ {
inConnection[i] = infinity
earliestArrival[i] = infinity
}
// main loop
earliestArrival[departureStation] = departureTime
for i, connection := range timetable {
if connection.departureTime >= earliestArrival[connection.departureStation] &&
connection.arrivalTime < earliestArrival[connection.arrivalStation] {
inConnection[connection.arrivalStation] = uint32(i)
earliestArrival[connection.arrivalStation] = connection.arrivalTime
}
}
// print result
if inConnection[arrivalStation] == infinity {
fmt.Println("NO_SOLUTION")
} else {
route := make([]Connection, 0)
for lastConnectionIndex := inConnection[arrivalStation]; lastConnectionIndex != infinity; {
connection := timetable[lastConnectionIndex]
route = append(route, connection)
lastConnectionIndex = inConnection[connection.departureStation]
}
for i := len(route) - 1; i >= 0; i-- {
fmt.Printf("%d %d %d %d\n", route[i].departureStation, route[i].arrivalStation, route[i].departureTime, route[i].arrivalTime)
}
fmt.Println("")
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
timetable := readTimetable(scanner)
var departureStation, arrivalStation, departureTime uint32
for scanner.Scan() {
if len(scanner.Text()) == 0 {
os.Exit(0)
} else {
_, err := fmt.Sscanln(scanner.Text(), &departureStation, &arrivalStation, &departureTime)
if err != nil {
panic(err)
}
timetable.compute(departureStation, arrivalStation, departureTime)
}
}
}