-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod.c
120 lines (97 loc) · 2.62 KB
/
method.c
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
#include <stdlib.h>
#include "method.h"
#define TRUE 1
#define FALSE 0
// Giant Tour
void giant_tour(struct graph_am* G, int f, int* T) {
int i;
int j;
float min; // minimum of the column
int ind_min; // index of the minimum
int* mark;
mark = (int*) malloc((G->n-1)*sizeof(int));
for (i=0;i<G->n-1;i++) {
mark[i] = FALSE;
}
// init the current client
int cur = f;
for (i=0;i<G->n-1;i++) {
// add the current client in the tour and mark it
T[i] = cur;
mark[cur-1] = TRUE;
// look for the nearest unmarked neighbour
ind_min = -1;
min = -1;
for (j=1;j<G->n;j++) {
if ((mark[j-1] == FALSE) && (G->M[cur+cur*(G->n-1)+j] != 0)) {
if ((G->M[cur+cur*(G->n-1)+j] < min) || (min == -1)) {
min = G->M[cur+cur*(G->n-1)+j];
ind_min = j;
}
}
}
// change the current client
cur = ind_min;
}
free(mark);
}
// Split
void split(struct graph_am* G, int Q, int* q, int* T, struct graph_al* H) {
int i;
int j;
int load;
float total_cost;
for (i=0;i<G->n-1;i++) {
j = i;
load = 0;
while ((j < G->n-1) && (load <= Q)) {
load += q[T[j]-1];
if (i == j) {
total_cost = G->M[T[i]] + G->M[T[i]+T[i]*(G->n-1)];
} else {
total_cost = total_cost
- G->M[T[j-1]+T[j-1]*(G->n-1)]
+ G->M[T[j-1]+T[j-1]*(G->n-1)+T[j]]
+ G->M[T[j]+T[j]*(G->n-1)];
}
if (load <= Q) {
add_edge_al(H, i, j+1, total_cost);
}
j += 1;
}
}
}
// Bellman-Ford
void bellman_ford(struct graph_al* H, float* p, int* F) {
int i;
int j;
int x;
int y;
// init potentials and fathers
p[0] = 0;
F[0] = 0;
for (i=1;i<H->n;i++) {
p[i] = -1;
F[i] = 0;
}
// for all successor of the root
for (i=H->h[0];i<H->h[1];i++) {
x = H->s[i];
p[x] = H->w[i];
}
// for all successor of other nodes
for (x=1;x<H->n-1;x++) {
for (j=H->h[x];j<H->h[x+1];j++) {
y = H->s[j];
if (p[x] + H->w[j] < p[y] || p[y] == -1) {
p[y] = p[x] + H->w[j];
F[y] = x;
}
}
}
// the last node has no successor
if (p[H->n-2] + H->w[H->h[H->n]-1] < p[H->n-1] || p[H->n-1] == -1) {
p[H->n-1] = p[H->n-2] + H->w[H->h[H->n]-1];
F[H->n-1] = H->n-2;
}
}