-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
160 lines (133 loc) · 3.61 KB
/
main.c
File metadata and controls
160 lines (133 loc) · 3.61 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "heap.c"
int d = 0; // numero nodi
int k = 0;
char* comando;
#define INF 2000000000
int addGraphAndComputeRank();
void topK();
void dijkstraQueue(int G[d][d], int s, int pred[], int dist[]);
int main() {
char continua = 1;
char buffer[21];
char d_str[10];
char k_str[10];
boolean costruisci_heap = 0;
if (fgets(buffer, 21, stdin)) {}
int i = 0, j = 0;
do {
d_str[i] = buffer[i];
i++;
} while (buffer[i] != ' ');
do {
i++;
k_str[j] = buffer[i];
j++;
} while (buffer[i] != '\0');
d = atoi(d_str);
k = atoi(k_str);
int dim_classifica = 0;
heapNode classifica[k];
int indiceGrafo = 0;
char comando[20];
do {
memset(comando, '\0', 20);
if(fgets(comando, 20, stdin)) {} else continua = 0;
if(comando[0] == 'A') {
heapNode newnode;
newnode.data = indiceGrafo;
newnode.priority = addGraphAndComputeRank();
indiceGrafo++;
if (dim_classifica < k) {
classifica[dim_classifica] = newnode;
dim_classifica++;
} else {
if (! costruisci_heap) {
build_max_heap(classifica, dim_classifica);
costruisci_heap = 1;
}
if(get_maxmin(classifica).priority > newnode.priority) {
classifica[0] = newnode;
max_heapify(classifica, 0, dim_classifica);
}
}
} else if(comando[0] == 'T') {
for (int l = 0; l < dim_classifica; ++l) {
printf("%d", classifica[l].data);
if (l != dim_classifica - 1)
printf(" ");
}
printf("\n");
}
} while(continua);
return 0;
}
int addGraphAndComputeRank() {
char str[11];
int G[d][d];
int k = 0, j = 0, l = 0;
char buffer[10*d];
for (int i = 0; i < d; i++) {
memset(buffer, '\0', 10*d);
if (fgets(buffer, 10*d, stdin)) {}
do {
str[j] = buffer[k];
k++;
j++;
if (buffer[k] == ',' || buffer[k] == '\n') {
G[i][l] = atoi(str);
memset(str, '\0', 11);
l++;
k++;
j = 0;
}
} while (buffer[k] != '\0');
l = 0;
j = 0;
k = 0;
}
int parent[d];
int dist[d];
dijkstraQueue(G, 0, parent, dist);
int metrica = 0;
for (int i = 0; i < d; ++i)
if(dist[i] != INF)
metrica += dist[i];
return metrica;
}
void dijkstraQueue(int G[d][d], int s, int pred[], int dist[]) {
int n = 0;
heapNode Q[d];
dist[s] = 0;
for (int i = 0; i < d; i++) {
if(i != s) {
dist[i] = INF;
}
heapNode node;
node.priority = dist[i];
node.data = i;
min_heap_insert(Q, node, &n);
}
int i = 0;
while (n != 0) {
heapNode u = delete_min(Q, &n);
for (int j = 0; j < d; ++j) {
if (G[u.data][j] != 0 && u.data != j) {
int ndis = dist[u.data] + G[u.data][j];
if (dist[j] > ndis) {
dist[j] = ndis;
pred[j] = u.data;
int l;
for (l = 0; l < n; ++l) {
if (Q[l].data == j) {
updatePriority(Q, n, l, ndis);
}
}
}
}
}
i++;
}
}