-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra_handle.cpp
More file actions
161 lines (150 loc) · 3.42 KB
/
Copy pathDijkstra_handle.cpp
File metadata and controls
161 lines (150 loc) · 3.42 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cmath>
using namespace std;
#define MAX 0
typedef struct arcnode
{
int index;
float weight;
arcnode *next;
} arcnode;
typedef struct vnode
{
int data;
arcnode *firstarc;
} vnode;
typedef struct
{
vnode adjlist[10010];
int n, m;
} graph;
typedef struct
{
float d;
// int parent;
int data;
} vertex;
vertex *u = NULL;
void heapify(int queue[], int queue_size, int i)
{
int l = 2 * i;
int r = 2 * i + 1;
int biggest = i;
if (l <= queue_size && u[queue[l]].d > u[queue[biggest]].d)
biggest = l;
if (r <= queue_size && u[queue[r]].d > u[queue[biggest]].d)
biggest = r;
if (biggest != i)
{
int temp = queue[i];
queue[i] = queue[biggest];
queue[biggest] = temp;
heapify(queue, queue_size, biggest);
}
return;
}
void init_queue(int *queue, int &queue_size)
{
for (int i = queue_size / 2; i >= 1; i--)
{
heapify(queue, queue_size, i);
}
}
void Relax(graph g, vertex *u, vertex *v, float w, int *queue, int &queue_size)
{
if ((u->d * w) > v->d)
{
v->d = (u->d * w);
init_queue(queue, queue_size);
return;
}
// cout << u->data<<' '<< u->d <<' '<< v->data << ' ' << v->d << endl;
return;
}
double Dijkstra(graph g, int start, int end)
{
u = new vertex[g.n];
int *queue = new int[g.n + 5];
for (int i = 0; i < g.n; i++)
{
u[i].d = MAX;
// u[i].parent = -1;
u[i].data = i;
queue[i + 1] = i;
}
u[start].d = 1;
int queue_size = g.n;
init_queue(queue, queue_size);
while (queue_size > 0)
{
vertex head = u[queue[1]];
queue[1] = queue[queue_size];
queue_size--;
heapify(queue, queue_size, 1);
// cout << g.adjlist[0].firstarc->index << endl;
arcnode *temp_node = g.adjlist[head.data].firstarc;
// cout<< temp_node->index << endl;
while (temp_node != NULL)
{
Relax(g, &head, &u[temp_node->index], temp_node->weight, queue, queue_size);
// if (flag)
// q.push(u[temp_node->index]);
temp_node = temp_node->next;
}
}
return u[end].d;
}
int main()
{
graph g;
cin >> g.n >> g.m;
for (int i = 0; i < g.n; i++)
{
g.adjlist[i].firstarc = NULL;
}
for (int i = 0; i < g.m; i++)
{
int u, v;
float w;
cin >> u >> v >> w;
arcnode *temp = new arcnode;
temp->index = v;
temp->weight = w;
temp->next = NULL;
if (g.adjlist[u].firstarc == NULL)
{
g.adjlist[u].firstarc = temp;
}
else
{
arcnode *p = g.adjlist[u].firstarc;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
arcnode *temp0 = new arcnode;
temp0->index = u;
temp0->weight = w;
temp0->next = NULL;
if (g.adjlist[v].firstarc == NULL)
{
g.adjlist[v].firstarc = temp0;
}
else
{
arcnode *p = g.adjlist[v].firstarc;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp0;
}
// cout<<g.adjlist[u].firstarc->index<<endl;
}
int start, end;
cin >> start >> end;
cout << Dijkstra(g, start, end) << endl;
return 0;
}