From ea945ff4ffae82bb1ee77ec28ce373ba4525d283 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Mon, 12 Nov 2018 12:18:25 -0500 Subject: Change graph description format Also adds a `genrand' program for testing purposes. --- main.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 14 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 1f27558..0064a92 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,7 @@ struct edge { }; struct node { + string name; vector neighbors; }; @@ -137,6 +138,15 @@ node &insert_node(map &graph, int id) return it->second; } +node &insert_node_if_new(map &graph, int id) +{ + map::iterator it = graph.find(id); + if(it == graph.end()) + return insert_node(graph, id); + else + return it->second; +} + void erase_edges(map &graph, int id_a, int id_b) { cerr << "Erasing edges " << id_a << "-" << id_b << endl; @@ -398,34 +408,57 @@ double simp_graph(map &graph, int s, int t) return graph[s].neighbors[0].weight; } +int get_node_id(map &names, string name, int &counter) +{ + const map::const_iterator it = names.find(name); + if(it == names.end()) + return names[name] = counter++; + else + return it->second; +} + int main() { /* construct graph from adjacency list */ map graph; string line; - int s, t; - cin >> s >> t; + map names; + string s, t; + + getline(cin, line); + stringstream ss(line); + ss >> s >> t; + + cerr << "Source and sink are " << s << ", " << t << endl; + + int id_counter = 1; while(getline(cin, line)) { - stringstream ss(line); + ss = stringstream(line); - int node_id; - struct node n; - ss >> node_id; + double weight; + string a, b; + ss >> a >> b >> weight; - struct edge e; - while(ss >> e.id && ss >> e.weight) - { - cerr << "Adding neighbor " << e.id << " with weight " << e.weight << endl; + int id_a, id_b; + id_a = get_node_id(names, a, id_counter); + id_b = get_node_id(names, b, id_counter); - n.neighbors.push_back(e); - } + insert_node_if_new(graph, id_a); + insert_node_if_new(graph, id_b); + + graph[id_a].name = a; + graph[id_b].name = b; - graph.insert(std::pair(node_id, n)); + insert_edge(graph, id_a, id_b, weight); } - double eq = simp_graph(graph, s, t); + int id_s, id_t; + id_s = names.at(s); + id_t = names.at(t); + + double eq = simp_graph(graph, id_s, id_t); cerr << "Equivalent resistance: " << eq << endl; } -- cgit v1.1