-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
131 lines (111 loc) · 3.13 KB
/
graph.h
File metadata and controls
131 lines (111 loc) · 3.13 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
const int INF = 0x30000000;
using PIS = std::pair<int, size_t>; // pair(dist, nodeID)
class Graph
{
public:
struct Edge
{
size_t to;
int weight;
Edge(size_t t, int w = 1) : to(t), weight(w) {}
};
private:
size_t vertexnum, edgenum;
std::vector<std::vector<Edge>> adjacencylist;
public:
Graph(size_t n) : vertexnum(n), edgenum(0) {adjacencylist.resize(n);}
size_t v() const {return vertexnum;}
size_t e() const {return edgenum;}
void print();
int len(size_t x, size_t y);
void connect(size_t a, size_t b, int weight);
const std::vector<Edge>& getneighbors(size_t i);
std::vector<int> dijkstra_simple(size_t i);
std::vector<int> dijkstra_optimized(size_t i);
};
void Graph::print()
{
for(size_t i = 0; i < vertexnum; i++)
{
std::cout << "Point " << i << ": ";
for(const auto& j : adjacencylist[i])
std::cout << j.to << " ";
std::cout << std::endl;
}
}
int Graph::len(size_t x, size_t y)
{
for(const auto& e : adjacencylist[x])
if(e.to == y)
return e.weight;
return INF;
}
void Graph::connect(size_t a, size_t b, int weight = 1)
{
adjacencylist[b].push_back(Edge(a, weight));
adjacencylist[a].push_back(Edge(b, weight));
edgenum++;
}
const std::vector<Graph::Edge>& Graph::getneighbors(size_t i)
{
return adjacencylist[i];
}
std::vector<int> Graph::dijkstra_simple(size_t i)
{
std::vector<int> dist(vertexnum, INF); dist[i] = 0;
std::vector<int> fixed(vertexnum, 0); fixed[i] = 1;
size_t prev = i;
for(size_t j = 1; j < vertexnum; j++)
{
for(const auto& e : adjacencylist[prev])
{
size_t dest = e.to; int len = e.weight;
if(fixed[dest] == 0)
dist[dest] = std::min(dist[dest], dist[prev] + len);
}
int index = -1, min = INF;
for(size_t k = 0; k < vertexnum; k++)
{
if(fixed[k] == 0)
{
if(dist[k] < min)
{
index = (int)k; min = dist[k];
}
}
}
if(index == -1)
return dist;
prev = (size_t)index; fixed[prev] = 1;
}
return dist;
}
std::vector<int> Graph::dijkstra_optimized(size_t i)
{
std::vector<int> dist(vertexnum, INF); dist[i] = 0;
std::priority_queue<PIS, std::vector<PIS>, std::greater<PIS>> pq;
pq.push(PIS(0, i));
while(!pq.empty())
{
int minlen = pq.top().first;
size_t start = pq.top().second;
pq.pop();
if(minlen > dist[start]) continue;
for(const auto& edge : adjacencylist[start])
{
size_t end = edge.to; int len = edge.weight;
if(len + minlen < dist[end])
{
dist[end] = len + minlen;
pq.push(PIS(dist[end], end));
}
}
}
return dist;
}