-
Notifications
You must be signed in to change notification settings - Fork 438
/
Bellman-Ford.cpp
124 lines (92 loc) · 1.58 KB
/
Bellman-Ford.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
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
#include <cstdio>
#include <cmath>
#define VERTEX_COUNT 10000
#define EDGE_COUNT 10000
#define INFINITE 1000000000
struct vertex
{
int first_edge;
int x, y;
double shortest;
}V[VERTEX_COUNT];
int vc = 1;
struct edge
{
int endp;
int next;
double w;
}E[EDGE_COUNT];
int ec = 1;
int ivc, iec;
int s, t;
void AddVertex(int x, int y)
{
V[vc].x = x;
V[vc].y = y;
vc++;
}
void AddEdge(int u, int v)
{
E[ec].next = V[u].first_edge;
V[u].first_edge = ec;
E[ec].endp = v;
E[ec].w = sqrt((V[u].x - V[v].x) * (V[u].x - V[v].x) + (V[u].y - V[v].y) * (V[u].y - V[v].y));
ec++;
}
// We assume that s is the source and t is the destination.
void InitialSingleSource()
{
V[s].shortest = 0;
for (int i = 1; i <= ivc; i++)
{
if (i != s)
{
V[i].shortest = INFINITE;
}
}
}
// The edge (u, v) must exist.
void Relax(int u, int v, double w)
{
if (w + V[u].shortest < V[v].shortest)
{
V[v].shortest = w + V[u].shortest;
}
}
// The main procedure of "Bellman-Ford" algorithm.
void Bellman_Ford()
{
InitialSingleSource();
for (int i = 0; i < ivc - 1; i++)
{
for (int curp = 1; curp <= ivc; curp++)
{
for (int cure = V[curp].first_edge; cure != 0; cure = E[cure].next)
{
Relax(curp, E[cure].endp, E[cure].w);
}
}
}
}
int main()
{
scanf("%d", &ivc);
for (int i = 0; i < ivc; i++)
{
int x, y;
scanf("%d%d", &x, &y);
AddVertex(x, y);
}
scanf("%d", &iec);
for (int i = 0; i < iec; i++)
{
int u, v;
scanf("%d%d", &u, &v);
AddEdge(u, v);
AddEdge(v, u);
}
scanf("%d%d", &s, &t);
Bellman_Ford();
printf("%.2lf", V[t].shortest);
return 0;
}