-
Notifications
You must be signed in to change notification settings - Fork 439
/
Bellman-Ford(Queue-Optimised).cpp
95 lines (82 loc) · 1.15 KB
/
Bellman-Ford(Queue-Optimised).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
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
#include <cstdio>
#define INF 1000000000
using namespace std;
struct vertex
{
int first_edge;
int dis;
}V[100010];
struct edge
{
int endp, next;
int w;
}E[1000010];
int ec = 1;
int ivc, iec;
int Q[200000];
int hp = 0, tp = 0;
void enqueue(int x)
{
Q[tp] = x;
tp++;
tp %= 200000;
}
int dequeue()
{
int ret = Q[hp];
hp++;
hp %= 200000;
return ret;
}
bool empty()
{
return hp == tp;
}
void add_edge(int u, int v, int w)
{
E[ec].next = V[u].first_edge;
V[u].first_edge = ec;
E[ec].endp = v;
E[ec].w = w;
ec++;
}
void initial_single_source(int s)
{
for (int i = 1; i <= ivc; i++)
{
V[i].dis = INF;
}
V[s].dis = 0;
}
void SPFA(int s)
{
initial_single_source(s);
enqueue(s);
while (!empty())
{
int u = dequeue();
for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next)
{
int newdis = V[u].dis + E[cur].w;
if (newdis < V[E[cur].endp].dis)
{
V[E[cur].endp].dis = newdis;
enqueue(E[cur].endp);
}
}
}
}
int main()
{
scanf("%d%d", &ivc, &iec);
for (int i = 0; i < iec; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
SPFA(1);
printf("%d", V[ivc].dis);
return 0;
}