-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjstraMod.cpp
More file actions
67 lines (56 loc) · 1.49 KB
/
Copy pathdjstraMod.cpp
File metadata and controls
67 lines (56 loc) · 1.49 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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
int main() {
int n, m;
cin >> n >> m;
vector<vector<pair<int,int>>> graph(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
graph[u].push_back({v, w});
}
vector<ll> dist(n, INF);
dist[0] = 0;
priority_queue<
pair<ll,int>,
vector<pair<ll,int>>,
greater<pair<ll,int>>
> pq;
pq.push({0, 0}); // (distance, node)
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
// 如果这个状态已经不是最优,丢掉
if (d > dist[u]) continue;
// 用 u 去松弛它的所有出边
for (auto [v, w] : graph[u]) {
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
// dist[i] 就是 0 → i 的最短路
}
/*
vector<bool> vis(n, false);
vector<int> dist(n, 1e9);
priority_queue<pair<int,int> > Q;
Q.push({0, 0});
dist[0] = 0;
while(!Q.empty()){
int u = Q.top().second;
Q.pop();
if(vis[u]) continue;
vis[u] = true;
for(auto [v, w] : G[u]){
if(dist[u] + w < dist[v]){
dist[v] = dist[u] + w;
Q.push({-dist[v], v});
}
}
}
return dist[n - 1] == 1e9 ? -1 : dist[n - 1];
*/