-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
118 lines (95 loc) · 2.55 KB
/
Graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Created by Бушев Дмитрий on 08.05.2021.
//
#ifndef FORDALGORITHM_GRAPH_H
#define FORDALGORITHM_GRAPH_H
#include <vector>
#include <map>
#include <ostream>
class Graph{
public:
typedef int Vid;
struct Vertex;
struct Weight{
int weight;
bool inf;
Weight(int w = 0, bool i = false): weight(w), inf(i){};
bool operator>(Weight const& another) const{
if(inf || another.inf)
return false;
return weight > another.weight;
}
bool operator<(Weight const& another) const{
if(inf || another.inf)
return false;
return weight < another.weight;
}
bool operator==(Weight const& another) const{
if(inf || another.inf)
return false;
return weight == another.weight;
}
bool operator!=(Weight const& another) const{
return !(*this == another);
}
Weight const& operator+=(Weight const& another) {
if(another.inf)
inf = true;
weight += another.weight;
return *this;
}
Weight const& operator-=(Weight const& another) {
if(another.inf)
inf = true;
weight -= another.weight;
return *this;
}
Weight operator+(Weight const& another) const {
Weight ret = *this;
ret += another;
return ret;
}
Weight operator-(Weight const& another) const{
Weight ret = *this;
ret -= another;
return ret;
}
};
struct Vertex{
std::map<Vid, Weight> edges;
};
private:
std::map<Vid, Vertex> vertices;
public:
void addVertex(Vid id);
void addEdge(Vid from, Vid to, int weight);
Graph::Weight getEdge(Vid from, Vid to) const;
bool hasVertex(Vid id) const;
size_t size() const{
return vertices.size();
}
auto begin(){
return vertices.begin();
}
auto begin() const{
return vertices.begin();
}
auto end() {
return vertices.end();
}
auto end() const{
return vertices.end();
}
auto at(size_t id){
return vertices.at(id);
}
auto at(size_t id) const{
return vertices.at(id);
}
void clear(){
vertices.clear();
}
};
std::ostream& operator<<(std::ostream& stream, Graph::Weight const& weight);
std::ostream& operator<<(std::ostream& stream, Graph const& graph);
#endif //FORDALGORITHM_GRAPH_H