-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.cpp
95 lines (74 loc) · 2.47 KB
/
3.cpp
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
#include <iostream>
#include <vector>
#include <list>
#include <utility>
#include <set>
#include <queue>
#include <unordered_set>
class IGraph {
public:
virtual ~IGraph() = default;
virtual void AddEdge(int from, int to, int weight) = 0;
virtual int VerticesCount() const = 0;
virtual std::vector<std::pair<int, int>> GetNextVertices(int vertex) const = 0;
};
class ListGraph : public IGraph {
public:
explicit ListGraph(int n) {
vertices.resize(n);
};
~ListGraph() override = default;
void AddEdge(int from, int to, int size) override {
vertices[from].push_back(std::make_pair(to, size));
vertices[to].push_back(std::make_pair(from, size));
};
int VerticesCount() const override {
return vertices.size();
};
std::vector<std::pair<int, int>> GetNextVertices(int vertex) const override {
std::vector<std::pair<int, int>> result(vertices[vertex].begin(), vertices[vertex].end());
return result;
};
private:
std::vector<std::vector<std::pair<int, int>>> vertices;
};
bool Relax(std::pair<int, int> &pair, int cour, std::vector<int> &visited) {
if (visited[pair.first] > visited[cour] + pair.second) {
visited[pair.first] = visited[cour] + pair.second;
return true;
}
return false;
}
int bfs(const IGraph &graph, int start, int end) {
//не макс инт поскольку есть условие на (visited[child.first] > visited[cour] + child.second) что больше диопазона
std::vector<int> visited(graph.VerticesCount(), 1000000);
std::set<std::pair<int, int>> q;
visited[start] = 0;
q.insert(std::make_pair(0, start));
while (!q.empty()) {
int cour = q.begin()->second;
q.erase(q.begin());
std::vector<std::pair<int, int>> children = graph.GetNextVertices(cour);
for (auto &child: children) {
if (visited[child.first] > visited[cour] + child.second) {
visited[child.first] = visited[cour] + child.second;
q.insert(std::make_pair(visited[child.first], child.first));
}
}
}
return visited[end];
}
int main() {
int N, M;
std::cin >> N >> M;
ListGraph list = ListGraph(N);
int from, to, size;
for (int i = 0; i < M; i++) {
std::cin >> from >> to >> size;
list.AddEdge(from, to, size);
}
int start, end;
std::cin >> start >> end;
std::cout << bfs(list, start, end);
return 0;
}