本文整理汇总了C++中graph_t::at方法的典型用法代码示例。如果您正苦于以下问题:C++ graph_t::at方法的具体用法?C++ graph_t::at怎么用?C++ graph_t::at使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph_t
的用法示例。
在下文中一共展示了graph_t::at方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: shortest_path
int shortest_path(const graph_t& g, const graph_t& g_r, uint32_t u, uint32_t v) {
// check if u exists and has outgoing edges
auto it = g.find(u);
if (it == g.end() || (it->second).size() == 0)
return -1;
// check if v exists and has incoming edges
it = g_r.find(v);
if (it == g_r.end() || (it->second).size() == 0)
return -1;
// find shortest path distance from u to v with a bidi-BFS
vector<uint32_t> f {u}; // forward fringe
vector<uint32_t> r {v}; // reverse fringe
unordered_map<uint32_t,bool> visited_f;
unordered_map<uint32_t,bool> visited_r;
visited_f[u] = true;
visited_r[v] = true;
int path_length_f = 0;
int path_length_r = 0;
while (f.size() > 0 && r.size() > 0) {
if (f.size() <= r.size()) {
// expand the forward fringe
/*cout << "f: ";
for (uint32_t i : f)
cout << i << " ";
cout << endl;*/
path_length_f++;
auto this_level = move(f); // f is now empty
for (auto node : this_level) {
if (g.find(node) == g.end())
continue;
for (auto neighbor : g.at(node)) {
if (!visited_f[neighbor]) {
f.push_back(neighbor);
visited_f[neighbor] = true;
}
if (visited_r[neighbor]) {
return path_length_f + path_length_r;
}
}
}
} else {
/*cout << "r: ";
for (uint32_t i : r)
cout << i << " ";
cout << endl;*/
path_length_r++;
auto this_level = move(r);
for (auto node : this_level) {
if (g_r.find(node) == g_r.end())
continue;
for (auto neighbor : g_r.at(node)) {
if (!visited_r[neighbor]) {
r.push_back(neighbor);
visited_r[neighbor] = true;
}
if (visited_f[neighbor]) {
return path_length_f + path_length_r;
}
}
}
}
}
return -1;
}