LiberOJ - 119. 单源最短路
D
i
j
s
k
t
r
a
,
v
e
c
t
o
r
存图
Dijsktra,vector存图
Dijsktra,vector存图
D
i
j
k
s
t
r
a
Dijkstra
Dijkstra 堆优化版 的 时间复杂度 为
O
(
m
l
o
g
n
)
O(mlogn)
O(mlogn)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 2510;
vector<PII> g[N];
int dist[N];
bool st[N];
int n, m, s, t;
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
priority_queue<PII, vector<PII>, greater<PII> > heap;
heap.push({0, s});
dist[s] = 0;
while (!heap.empty())
{
auto [distance, ver] = heap.top();
heap.pop();
st[ver] = true;
for (auto [j, w]: g[ver])
{
if (dist[j] > distance + w)
{
dist[j] = distance + w;
heap.push({dist[j], j});
}
}
}
}
int main()
{
cin >> n >> m >> s >> t;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
dijkstra();
cout << dist[t] << endl;
return 0;
}