-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1480 - Dota Warlock Power.c
268 lines (205 loc) · 5.27 KB
/
1480 - Dota Warlock Power.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MaxVertices 110
#define Infinity 9999
#define White 'w'
#define Grey 'g'
#define Black 'b'
typedef struct gEdge{
int child;
double weight;
struct gEdge *nextEdge;
}GEdge, *GEdgePtr;
typedef struct gvertex{
char colour;
int id, parent, discover, finish, inDegree;
double cost;
GEdgePtr firstEdge;
}GVertex;
typedef struct graph {
int numV;
GVertex vertex[MaxVertices + 1];
} *Graph;
double calculate_distance(double, double, double, double);
GVertex newGVertex(int);
GEdgePtr newGEdge(int, double);
Graph newGraph(int );
void buildGraph(FILE*,Graph G);
void addEdge(int, int, double, Graph );
void siftUp(Graph , int heap[], int, int[]);
void siftDown(Graph, int, int[], int, int, int[]);
void Prim(Graph, int);
void printMST(Graph);
int main()
{
FILE * in = fopen("input.txt", "r");
int numVerticies;
fscanf(in,"%d", &numVerticies);
Graph G = newGraph(numVerticies);
buildGraph(in,G);
Prim(G, 1);
fclose(in);
printf("\n\n\t***** COJ : 1480 - Dota Warlock Power *****\n\n");
system("PAUSE");
return 0;
}
// Functions
// length of a line
double calculate_distance(double x1, double y1, double x2, double y2)
{
double distance_x = x1 - x2;
double distance_y = y1 - y2;
return sqrt((distance_x* distance_x) + (distance_y * distance_y));
}
////////////////////////////////////////////////////////////////////////////////////
// GRAPH FUNCTIONS FOR PRIM
GVertex newGVertex(int name){
GVertex temp;
temp.id = name;
temp.firstEdge = NULL;
return temp;
}
GEdgePtr newGEdge(int c, double w){
GEdgePtr p = (GEdgePtr)malloc(sizeof(GEdge));
p->child = c;
p->weight = w;
p->nextEdge = NULL;
return p;
}
Graph newGraph(int n){
if (n > MaxVertices)
{
printf("\n Too big Only %d vertices's allowed. \n", MaxVertices);
system("pause");
exit(1);
}
Graph p = (Graph)malloc(sizeof(struct graph));
p->numV = n;
return p;
}
// build graph
void buildGraph(FILE*in,Graph G){
int j, k;
double xy[MaxVertices][2]; // used to store the coordinates X and Y
for (j = 1; j <= G->numV; j++)
for (k = 1; k <= G->numV; k++)
{
if (j == 1)
{
G->vertex[k] = newGVertex(0);
G->vertex[k].id = k;
G->vertex[k].cost = Infinity;
G->vertex[k].parent = 0;
G->vertex[k].colour = White; // to Black when vertex is added to MST
fscanf(in, "%lf %lf", &xy[k][0], &xy[k][1]); // scanning the X and Y values to be used
}
if (j != k)
// cal the length of a line to be used as the weight
addEdge(j, k, calculate_distance(xy[j][0], xy[j][1], xy[k][0], xy[k][1]), G);
}
}
// AddEdge
void addEdge(int X, int Y, double weight, Graph G){
int j, k;
for (j = 1; j <= G->numV; j++)
if ((X == G->vertex[j].id) ) break;
for (k = 1; k <= G->numV; k++)
if ((Y == G->vertex[k].id) ) break;
if (j > G->numV || k > G->numV)
{
printf("No Such edge: %d -> %d\n", X, Y);
system("pause");
exit(1);
}
GEdgePtr ep = newGEdge(k, weight);
GEdgePtr prev, curr;
prev = curr = G->vertex[j].firstEdge;
while (curr != NULL && (Y > G->vertex[curr->child].id))
{
prev = curr;
curr = curr->nextEdge;
}
if (prev == curr)
{
ep->nextEdge = G->vertex[j].firstEdge;
G->vertex[j].firstEdge = ep;
}
else
{
ep->nextEdge = curr;
prev->nextEdge = ep;
}
}
// sift up
void siftUp(Graph G, int heap[], int n, int heapLoc[]){
int siftItem = heap[n];
int child = n;
int parent = child / 2;
while (parent > 0)
{
if (G->vertex[siftItem].cost >= G->vertex[heap[parent]].cost) break;
heap[child] = heap[parent];
heapLoc[heap[parent]] = child;
child = parent;
parent = child / 2;
}
heap[child] = siftItem;
heapLoc[siftItem] = child;
}
// siftDown
void siftDown(Graph G, int key, int heap[], int root, int last, int heapLoc[]){
int smaller = 2 * root;
while (smaller <= last)
{
if (smaller < last)
if (G->vertex[heap[smaller + 1]].cost < G->vertex[heap[smaller]].cost)
smaller++;
if (G->vertex[key].cost <= G->vertex[heap[smaller]].cost) break;
heap[root] = heap[smaller];
heapLoc[heap[smaller]] = root;
root = smaller;
smaller = 2 * root;
}
heap[root] = key;
heapLoc[key] = root;
}
// Prim algorithm
void Prim(Graph G, int s){
//perform Prims's algorithm on G starting with vertex s
int j, heap[MaxVertices + 1], heapLoc[MaxVertices + 1];
G->vertex[s].cost = 0;
for (j = 1; j <= G->numV; j++) heap[j] = heapLoc[j] = j;
heap[1] = s; heap[s] = 1; heapLoc[s] = 1; heapLoc[1] = s;
int heapSize = G->numV;
while (heapSize > 0)
{
int u = heap[1];
if (G->vertex[u].cost == Infinity) break;
G->vertex[u].colour = Black;
//reorganize heap after removing top item
siftDown(G, heap[heapSize], heap, 1, heapSize - 1, heapLoc);
GEdgePtr p = G->vertex[u].firstEdge;
while (p != NULL)
{
if (G->vertex[p->child].colour == White && p->weight < G->vertex[p->child].cost)
{
G->vertex[p->child].cost = p->weight;
G->vertex[p->child].parent = u;
siftUp(G, heap, heapLoc[p->child], heapLoc);
}
p = p->nextEdge;
}
--heapSize;
}// end while
printMST(G);
}// end Prim's
void printMST(Graph G){
int j; double costMST = 0;
for (j = 2; j <= G->numV; j++)
costMST += G->vertex[j].cost;
FILE*out = fopen("output.txt", "w");
fprintf(out,"The damage received by Warlock's enemies : %.2f\n", costMST * 5);
fclose(out);
}// end printMST