-
Notifications
You must be signed in to change notification settings - Fork 0
/
frog.c
139 lines (134 loc) · 3.76 KB
/
frog.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
#define INF 999999
typedef struct Node{
int serialNum;
float distance;
}HeapNode, *PHeapNode;
typedef struct Heap{
PHeapNode *HeapArray;
int maxSize;
int length;
}MinHeap, *PMinHeap;
void InitHeap(PMinHeap minHeap, int maxSize);
int CheckHeap(PMinHeap minHeap);
void ClearHeap(PMinHeap minHeap);
void InsertHeap(PMinHeap minHeap, PHeapNode NewNode);
void ShiftDown(PMinHeap minHeap, int i);
PHeapNode DeleteMin(PMinHeap minHeap);
float find(int (*coor)[2], int m);
float **compute(int (*coor)[2], int m);
int main(void){
int i, m, k = 0;
while (scanf("%d", &m) != EOF && m != 0){
int (*coor)[2] = (int (*)[2])malloc(m * 2 * sizeof(int));
for (i = 0; i < m; i++){
scanf("%d", *(coor + i));
scanf("%d", *(coor + i) + 1);
}
printf("%.3f\n", find(coor, m));
}
return 0;
}
float find(int (*coor)[2], int m){
int i;
float **c = compute(coor, m);
float *dist = (float *)malloc(m * sizeof(float));
for (i = 0; i < m; i++) dist[i] = INF;
PMinHeap minHeap = (PMinHeap)malloc(sizeof(MinHeap));
InitHeap(minHeap, MAXSIZE);
PHeapNode source = (PHeapNode)malloc(sizeof(HeapNode));
source->serialNum = 0;
source->distance = 0;
dist[0] = 0;
InsertHeap(minHeap, source);
while (!CheckHeap(minHeap)){
PHeapNode cur = DeleteMin(minHeap);
for (i = 0; i < m; i++){
if (i == cur->serialNum) continue;
float max;
if (c[cur->serialNum][i] > cur->distance) max = c[cur->serialNum][i];
else max = cur->distance;
if (max < dist[i]){
dist[i] = max;
PHeapNode liveNode = (PHeapNode)malloc(sizeof(HeapNode));
liveNode->serialNum = i;
liveNode->distance = dist[i];
InsertHeap(minHeap, liveNode);
}
}
free(cur);
}
return dist[1];
}
float **compute(int (*coor)[2], int m){
int i, j;
float **c = (float **)malloc(m * sizeof(float *));
for (i = 0; i < m; i++) *(c + i) = (float *)malloc(m * sizeof(float));
for (i = 0; i < m; i++)
for (j = 0; j < m; j++){
int dx = (*(coor + i))[0] - (*(coor + j))[0];
int dy = (*(coor + i))[1] - (*(coor + j))[1];
c[i][j] = sqrt(dx * dx + dy * dy);
}
return c;
}
void InitHeap(PMinHeap minHeap, int maxSize){
minHeap->HeapArray = (PHeapNode *)malloc(maxSize * sizeof(PHeapNode));
if (minHeap->HeapArray == NULL) exit(-1);
minHeap->maxSize = maxSize;
minHeap->length = 0;
}
int CheckHeap(PMinHeap minHeap){
if (minHeap->length == 0) return TRUE;
else return FALSE;
}
void ClearHeap(PMinHeap minHeap){
if (minHeap != NULL){
minHeap->length = 0;
}
}
void InsertHeap(PMinHeap minHeap, PHeapNode NewNode){
int i, j;
if (minHeap->length == minHeap->maxSize){
minHeap->HeapArray = (PHeapNode *)realloc(minHeap->HeapArray, 2 * minHeap->maxSize * sizeof(PHeapNode));
minHeap->maxSize *= 2;
}
minHeap->HeapArray[minHeap->length] = NewNode;
minHeap->length++;
i = minHeap->length - 1;
while (i != 0){
j = (i - 1) / 2;
if (NewNode->distance > minHeap->HeapArray[j]->distance) break;
PHeapNode temp = minHeap->HeapArray[i];
minHeap->HeapArray[i] = minHeap->HeapArray[j];
minHeap->HeapArray[j] = temp;
i = j;
}
}
void ShiftDown(PMinHeap minHeap, int i){
int m = minHeap->length / 2;
while (i < m){
int r = 2 * i + 2;
int l = 2 * i + 1;
if ((r < minHeap->length) && (minHeap->HeapArray[r]->distance < minHeap->HeapArray[l]->distance))
l = r;
if (minHeap->HeapArray[i]->distance < minHeap->HeapArray[l]->distance) return;
PHeapNode temp = minHeap->HeapArray[i];
minHeap->HeapArray[i] = minHeap->HeapArray[l];
minHeap->HeapArray[l] = temp;
i = l;
}
}
PHeapNode DeleteMin(PMinHeap minHeap){
int m = minHeap->length - 1;
PHeapNode temp = minHeap->HeapArray[0];
minHeap->HeapArray[0] = minHeap->HeapArray[m];
if (m != 0) ShiftDown(minHeap, 0);
minHeap->length--;
return temp;
}