https://hihocoder.com/problemset/problem/1093
实现参考了算法导论。 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using namespace std;
typedef long long LL;
const int MAX_N = 100000 + 16;
const int MAX_M = 1000000 + 16;
const int INF = 0x3F3F3F3F;
struct Edge {
int u, v, c;
Edge() {}
Edge(int u, int v, int c) : u(u), v(v), c(c) {}
};
struct Graph {
vector<int> g[MAX_N];
vector<Edge> edges;
int n;
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++)
g[i].clear();
edges.clear();
}
void add(int u, int v, int c) {
edges.push_back(Edge(u, v, c));
g[u].push_back(edges.size() - 1);
}
} graph;
struct BellmanFord {
int dist[MAX_N];
void init(int n, int s) {
for (int i = 0; i <= n; i++)
dist[i] = INF;
dist[s] = 0;
}
void work(const Graph &g, int s) {
init(g.n, s);
for (int i = 1; i < g.n; i++) {
for (const Edge &e : g.edges) {
dist[e.v] = min(dist[e.v], dist[e.u] + e.c);
}
}
}
} bellman_ford;
struct Dijkstra {
int dist[MAX_N];
bool vis[MAX_N];
void init(int n, int s) {
for (int i = 0; i <= n; i++) {
dist[i] = INF;
vis[i] = false;
}
dist[s] = 0;
}
int extract_min(int n) {
int d = INF;
int u = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i] && dist[i] < d) {
d = dist[i];
u = i;
}
}
return u;
}
void work(const Graph &g, int s) {
init(g.n, s);
int left = g.n;
while (left--) {
int u = extract_min(g.n);
vis[u] = true;
for (int i = 0; i < g.g[u].size(); i++) {
const Edge &e = g.edges[g.g[u][i]];
dist[e.v] = min(dist[e.v], dist[e.u] + e.c);
}
}
}
} dijkstra;
struct DijkstraPremium {
struct Node {
int u, d;
Node() {}
Node(int u, int d) : u(u), d(d) {}
bool operator>(const Node &rhs) const {
return this->d > rhs.d;
}
};
int dist[MAX_N];
bool vis[MAX_N];
priority_queue<Node, vector<Node>, greater<Node>> pq;
void init(int n, int s) {
for (int i = 0; i <= n; i++) {
dist[i] = INF;
vis[i] = false;
}
dist[s] = 0;
while (!pq.empty())
pq.pop();
pq.push(Node(s, dist[s]));
}
void work(const Graph &g, int s) {
init(g.n, s);
while (!pq.empty()) {
Node node = pq.top();
pq.pop();
int u = node.u;
if (vis[u])
continue;
vis[u] = true;
for (int i = 0; i < g.g[u].size(); i++) {
const Edge &e = g.edges[g.g[u][i]];
dist[e.v] = min(dist[e.v], dist[e.u] + e.c);
pq.push(Node(e.v, dist[e.v]));
}
}
}
} dijkstra_premium;
int main(int argc, char **argv) {
int n, m, s, t;
while (scanf("%d%d%d%d", &n, &m, &s, &t) != EOF) {
graph.init(n);
for (int i = 0; i < m; i++) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
graph.add(u, v, c);
graph.add(v, u, c);
}
dijkstra_premium.work(graph, s);
printf("%d\n", dijkstra_premium.dist[t]);
}
return 0;
}
最短路算法
- Post link: https://torapture.github.io/2019/11/05/最短路算法/
- Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.