本文整理汇总了C++中graph::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::clear方法的具体用法?C++ graph::clear怎么用?C++ graph::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph
的用法示例。
在下文中一共展示了graph::clear方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: read_edgelist
bool CDLib::read_edgelist(graph& g, const string& filepath) {
g.clear();
ifstream ifs;
ifs.open(filepath.c_str());
if (ifs.is_open()) {
vector<string> units;
double weight;
while (!ifs.eof()) {
weight = 1;
string line;
getline(ifs, line);
if ((line.size() > 0) && (line[0] != '#')) {
split(line, units);
if (units.size() != 0) {
if ((units.size() < 2) || (units.size() > 3)) return false;
if (units.size() == 3) weight = str2T<double>(units[2]);
g.add_node(units[0]);
g.add_node(units[1]);
g.add_edge(units[0], units[1], weight);
}
}
}
g.set_graph_name(filename(filepath));
return true;
}
return false;
}
示例2: init_empty_graph
void CDLib::init_empty_graph(graph& g, size_t size) {
g.clear();
if (g.is_directed()) g.convert_to_undirected();
if (g.is_weighted()) g.convert_to_unweighted(0);
for (id_type i = 0; i < size; i++) g.add_node();
}
示例3: read_matlab_sp
bool CDLib::read_matlab_sp(graph& g, const string& filepath) {
g.clear();
ifstream ifs;
ifs.open(filepath.c_str());
if (ifs.is_open()) {
id_type from, to;
double weight = 1;
while (!ifs.eof()) {
ifs >> from >> to >> weight;
while (max(from, to) > g.get_num_nodes()) {
g.add_node();
}
g.add_edge(from - 1, to - 1, weight);
}
g.set_graph_name(filename(filepath));
return true;
}
示例4: read_adjacencylist
bool CDLib::read_adjacencylist(graph& g, const string& filepath) {
g.clear();
ifstream ifs;
ifs.open(filepath.c_str());
if (ifs.is_open()) {
int type = 0;
id_type nid = 0, estart = 0;
string line;
getline(ifs, line);
vector<id_type> units;
split(line, units);
if ((units.size() > 2) && (units[0] == 0) && (units[1] == units.size() - 2)) {
type = 0;
estart = 2;
} else if ((units.size() > 1) && (units[0] == 0)) {
type = 1;
estart = 1;
} else {
type = 2;
estart = 0;
}
g.add_node(to_string(nid));
for (id_type i = estart; i < units.size(); i++) {
g.add_node(to_string(units[i]));
g.add_edge(to_string(nid), to_string(units[i]), 1);
}
while (!ifs.eof()) {
string line;
getline(ifs, line);
vector<id_type> units;
split(line, units);
if (units.size() > 0) {
if ((type == 0) || (type == 1)) nid = units[0];
else nid++;
g.add_node(to_string(nid));
for (id_type i = estart; i < units.size(); i++) {
g.add_node(to_string(units[i]));
g.add_edge(to_string(nid), to_string(units[i]), 1);
}
}
}
g.set_graph_name(filename(filepath));
return true;
}
return false;
}
示例5: main
int main() {
while(true) {
//scanf("%d\n", &M);
cin >> M;
if(M == 0) break;
//scanf("%s %s\n", name, dest);
cin >> name >> dest;
map<string, int> index;
index[name] = start = index.size() - 1;
index[dest] = end = index.size() - 1;
D(G.size());
G.resize(2);
for(int i = 0; i < M; i++) {
//scanf("%s %s %s\n", name, dest, word);
cin >> name >> dest >> word;
if(index.find(name) == index.end()) {
index[name] = index.size() - 1;
G.resize(G.size()+1);
}
if(index.find(dest) == index.end()) {
index[dest] = index.size() - 1;
G.resize(G.size()+1);
}
int a = index[name], b = index[dest];
//D(a); D(b);
G[a].push_back(ici(b, word[0], word.length()));
G[b].push_back(ici(a, word[0], word.length()));
//D(name); D(dest); D(word);
}
int ans = dijkstraHeap();
if(ans == INF) puts("impossivel");
else printf("%d\n", ans);
G.clear();
}
}
示例6: reset
void reset() {
g.clear();
g.resize(size);
}
示例7: main
int main(){
int w,h;
int mazeNo = 1;
while (cin >> w >> h && w && h){
g.clear();
d.clear();
longest = -1;
qty = 0;
for (int i=0; i<w; ++i){
for (int j=0; j<h; ++j){
g[node(2*i, j)].insert(node(2*i, j-1));
g[node(2*i, j-1)].insert(node(2*i, j));
g[node(2*i+1, j)].insert(node(2*i+1, j+1));
g[node(2*i+1, j+1)].insert(node(2*i+1, j));
char c;
cin >> c;
if (c == '\\'){
g[node(2*i+1, j)].insert(node(2*i, j));
g[node(2*i, j)].insert(node(2*i+1, j));
g[node(2*i, j)].insert(node(2*i+1, j));
g[node(2*i+1, j)].insert(node(2*i, j));
}else if (c == '/'){
g[node(2*i,j)].insert(node(2*i-1, j));
g[node(2*i-1,j)].insert(node(2*i, j));
g[node(2*i+1,j)].insert(node(2*i+2, j));
g[node(2*i+2,j)].insert(node(2*i+1, j));
}else{
cerr << "Unrecognized char in input" << endl;
}
}
}
/*for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){
printf("Vecinos de (%d, %d):\n", i->first.first, i->first.second);
set<node> v = i->second;
for (set<node>::iterator j = v.begin(); j != v.end(); ++j){
printf("(%d, %d) ", j->first, j->second);
}
cout << endl;
}*/
for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){
if (d.count(i->first) == 0){
d[i->first] = 0;
dfs(i->first);
}
}
printf("Maze #%d:\n", mazeNo++);
if (qty == 0){
printf("There are no cycles.\n");
}else{
printf("%d Cycles; the longest has length %d\n", qty, longest);
}
printf("\n");
}
return 0;
}