fork(1) download
  1. #include <unordered_map>
  2. #include <iostream>
  3. #include <string>
  4. #include <list>
  5. using namespace std;
  6.  
  7. struct node;
  8. struct edge
  9. {
  10. node &to;
  11. double weight;
  12. edge(node &to,double weight):to(to),weight(weight) {}
  13. };
  14. struct node
  15. {
  16. string name;
  17. list<edge> neighbors;
  18. };
  19. typedef unordered_map<string,node> graph;
  20.  
  21. node &find(graph &g,const string &name)
  22. {
  23. node &ret=g[name];
  24. ret.name=name;
  25. return ret;
  26. }
  27.  
  28. void append(graph &g,const string &a,const string &b,double weight)
  29. {
  30. node &A=find(g,a),&B=find(g,b);
  31. A.neighbors.push_back(edge(B,weight));
  32. B.neighbors.push_back(edge(A,weight)); // jeżeli nieskierowany
  33. }
  34.  
  35. int main()
  36. {
  37. graph g;
  38. append(g,"Aleksandrow","Ksawerow",10);
  39. append(g,"Aleksandrow","JanowLubelski",20);
  40. append(g,"Zglowiaczka","Ksawerow",30);
  41. append(g,"Ksawerow","Lodz",40);
  42. append(g,"Lodz","Zakopane",50);
  43. append(g,"Lodz","JanowLubelski",60);
  44. append(g,"JanowLubelski","Lodz",70);
  45. for(auto &node:g)
  46. {
  47. cout<<node.first<<endl;
  48. for(auto &e:node.second.neighbors) cout<<'\t'<<e.to.name<<" -> "<<e.weight<<endl;
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
Zakopane
	Lodz -> 50
Lodz
	Ksawerow -> 40
	Zakopane -> 50
	JanowLubelski -> 60
	JanowLubelski -> 70
Aleksandrow
	Ksawerow -> 10
	JanowLubelski -> 20
Ksawerow
	Aleksandrow -> 10
	Zglowiaczka -> 30
	Lodz -> 40
JanowLubelski
	Aleksandrow -> 20
	Lodz -> 60
	Lodz -> 70
Zglowiaczka
	Ksawerow -> 30