Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <map>
- using namespace std;
- class node {
- public:
- typedef pair<int, node*> ve;
- vector<ve> adj;
- string name;
- node (string s) : name(s) {}
- };
- class Graph {
- public:
- typedef map<string, node *> vmap;
- vmap map;
- void addVertex(const string& name);
- void addEdge(const string& from, const string& to, double cost);
- void printNodes();
- };
- void Graph::addVertex(const string &name) {
- vmap::iterator it = map.find(name);
- if (it == map.end()) {
- node *n;
- n = new node(name);
- map[name] = n;
- return;
- }
- cout << "existing node" << endl;
- }
- void Graph::addEdge(const string& from, const string& to, double cost) {
- node *f = map.find(from)->second;
- node *t = map.find(to)->second;
- pair<int, node *> edge = make_pair(cost, t);
- f->adj.push_back(edge);
- }
- void Graph::printNodes() {
- for (vmap::iterator it = map.begin(); it!= map.end(); it++) {
- cout << "Key: " << it->first;
- for (int i = 0; i < it->second->adj.size(); i++ ) {
- cout << " with edges: " << it->second->adj[i];
- }
- cout << endl;
- }
- }
- int main() {
- Graph graph;
- string input;
- for (int i = 0; i < 5; i++) {
- cin >> input;
- graph.addVertex(input);
- }
- graph.addEdge("nqma", "takuv", 0);
- graph.addEdge("nqma", "key", 0);
- graph.addEdge("nqma", "ei", 0);
- graph.addEdge("ei", "tupak", 0);
- graph.printNodes();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement