-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcWing850.cpp
65 lines (65 loc) · 1.33 KB
/
AcWing850.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
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 150010;
//w数组用于存储边的权重
int h[N], w[N], e[N], ne[N], dist[N], st[N];
int n, m, idx, x, y, z;
typedef pair<int, int> PII;
void add(int x, int y, int z)
{
e[idx] = y;
w[idx] = z;
ne[idx] = h[x];
h[x] = idx++;
}
int Dijkstra()
{
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>>heap;
//放入初始编号为1的结点,距离为0
heap.push({0, 1});
while (!heap.empty())
{
auto t = heap.top();
heap.pop();
int distance = t.first;
int num = t.second;
if (st[num])
{
continue;
}
for (int i = h[num]; i != -1; i = ne[i])
{
int v = e[i];
if (st[v])
{
continue;
}
if(dist[v] > w[i] + distance)
{
dist[v] = w[i] + distance;
heap.push({dist[v], v});
}
}
st[num] = true;
}
if (dist[n] == 0x3f3f3f3f)
{
return -1;
}
return dist[n];
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof(h));
for (int i = 0; i < m; i++)
{
cin >> x >> y >> z;
add(x, y, z);
}
cout<<Dijkstra();
}