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; }
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(); }
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; }
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; }
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(); } }
void reset() { g.clear(); g.resize(size); }
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; }