27 const int num_nodes, absl::Span<const ArcWithLength> arcs_with_length,
28 int source,
int destination) {
31 using ArcIndex = GraphType::ArcIndex;
33 GraphType graph(num_nodes, arcs_with_length.size());
34 std::vector<double> arc_lengths;
35 arc_lengths.reserve(arcs_with_length.size());
36 for (
const auto&
arc : arcs_with_length) {
37 graph.AddArc(
arc.tail,
arc.head);
38 arc_lengths.push_back(
arc.length);
40 std::vector<ArcIndex> permutation;
41 graph.Build(&permutation);
44 std::vector<ArcIndex> inverse_permutation(permutation.size());
45 if (!permutation.empty()) {
46 for (
ArcIndex i = 0; i < permutation.size(); ++i) {
47 inverse_permutation[permutation[i]] = i;
51 const absl::StatusOr<std::vector<NodeIndex>> topological_order =
53 CHECK_OK(topological_order) <<
"arcs_with_length form a cycle.";
56 &graph, &arc_lengths, *topological_order);
60 if (!shortest_path_on_dag.
IsReachable(destination)) {
62 .arc_path = std::vector<int>(),
63 .node_path = std::vector<int>()};
66 std::vector<int> arc_path = shortest_path_on_dag.
ArcPathTo(destination);
67 if (!inverse_permutation.empty()) {
68 for (
int i = 0; i < arc_path.size(); ++i) {
69 arc_path[i] = inverse_permutation[arc_path[i]];
75 .node_path = shortest_path_on_dag.
NodePathTo(destination)};